Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以在Typescript中创建一个实现接口的类,并且它';它还能有装饰师吗?_Typescript_Decorator - Fatal编程技术网

我可以在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

我可以在Typescript中创建一个实现接口的类,并且它仍然可以有装饰器吗

我有一门课

@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不客气。如果有帮助,请随意投票;-)我没有足够的声望投票:(当我得到它时,我会回来投票