【发布时间】:2021-02-04 00:59:11
【问题描述】:
我想将类属性指定为一种函数。但是,函数签名可能会有很大差异。
export class MyClass {
prop1: string;
prop2: AnotherClass;
prop3: function; // ?
example1() {
this.prop3 = () => 'string';
}
example2() {
this.prop3 = (a, b, c) => new Promise(...);
}
example3() {
this.prop3 = (a) => {
prop: () => 'str'
};
}
}
在prop3 定义它的值应该是函数类型的最短方法是什么?该属性很可能是一个粗箭头函数,所以我认为接口不会起作用,但也许泛型会起作用?
【问题讨论】:
标签: typescript function type-hinting