使用此代码,我在m1和m2处获得语法错误,基本上说:Type 'T' is not assignable to Type 'boolean'
或Type 'T' is not assignable to Type 'string'
interface myMethod {
<T>(b: T): myObject<T>;
}
class myObject<T> {
constructor(private gg: T) {
}
}
var m1: myMethod = function (b : boolean) {
return new myObject(b);
}
var m2: myMethod = function (b: string) {
return new myObject(b);
}
我该怎么办?
答案 0 :(得分:1)
myMethod
定义的签名是通用的,因此只有通用函数才能满足签名,或者用T
替换var m1: myMethod = function <T>(b : T) {
return new myObject(b);
}
// Works but I would not recommend id.
var m2: myMethod = function (b : any) {
return new myObject(b);
}
的签名。
enquo/!!