我可以在Typescript中创建一个实现接口的类,并且它';它还能有装饰师吗?
我可以在Typescript中创建一个实现接口的类,并且它仍然可以有装饰器吗 我有一门课我可以在Typescript中创建一个实现接口的类,并且它';它还能有装饰师吗?,typescript,decorator,Typescript,Decorator,我可以在Typescript中创建一个实现接口的类,并且它仍然可以有装饰器吗 我有一门课 @MyDecoratorA() export default class Foo { @MyDecoratorB() public mySearchFunc(source: string, subString: string) { /*a lot of logic here*/ return true; } } 我有我的界面 interface
@MyDecoratorA()
export default class Foo {
@MyDecoratorB()
public mySearchFunc(source: string, subString: string) {
/*a lot of logic here*/
return true;
}
}
我有我的界面
interface SearchFunc {
(source: string, subString: string): boolean;
}
我可以使实现myInterface的函数如下所示:
let mySearch: SearchFunc;
mySearch = function(source: string, subString: string) {
let result = source.search(subString);
return result > -1;
}
我正在尝试使类'Foo'的方法mySearchFunc实现接口'myInterface',但不丢失decorator
将来我将需要此装饰程序。您可以将类
Foo
上的mySearchFunc
方法的声明替换为字段声明,其中字段的类型是接口,并将方法实现分配给此字段:
interface SearchFunc {
(source: string, subString: string): boolean;
}
@MyDecoratorA()
export default class Foo
{
@MyDecoratorB()
public mySearchFunc : SearchFunc = (source: string, subString: string) => {
/*a lot of logic here*/
return true;
}
}
var foo = new Foo();
// You can use the field the same way as if it was declared as method
foo.mySearchFunc("a", "b");
除了箭头函数()=>{}
之外,还可以使用函数
表示法:
@MyDecoratorA()
export default class Foo
{
@MyDecoratorB()
public mySearchFunc : SearchFunc = function(source: string, subString: string) {
/*a lot of logic here*/
return true;
}
}
您可以将类
Foo
上的mySearchFunc
方法的声明替换为字段声明,其中字段的类型是接口,并将方法实现分配给此字段:
interface SearchFunc {
(source: string, subString: string): boolean;
}
@MyDecoratorA()
export default class Foo
{
@MyDecoratorB()
public mySearchFunc : SearchFunc = (source: string, subString: string) => {
/*a lot of logic here*/
return true;
}
}
var foo = new Foo();
// You can use the field the same way as if it was declared as method
foo.mySearchFunc("a", "b");
除了箭头函数()=>{}
之外,还可以使用函数
表示法:
@MyDecoratorA()
export default class Foo
{
@MyDecoratorB()
public mySearchFunc : SearchFunc = function(source: string, subString: string) {
/*a lot of logic here*/
return true;
}
}
@不客气。如果有帮助的话,请随意投票;-)我没有足够的声誉来投票:(当我得到它时,我会回来投票upvote@AlexSilva不客气。如果有帮助,请随意投票;-)我没有足够的声望投票:(当我得到它时,我会回来投票