我们用可选实现了一个泛型类型,它使用默认类型,否则在运行方法时应该使用类型集。
type Test = {
a: number;
b: string;
}
interface IApple {
method<T=Test>(): Promise<T>
}
现在,当我写这样一节课的时候。
class Apple implements IApple {
method() {
return Promise.resolve({
a: 10,
b: 'test',
} as Test)
}
获取类型错误
Property 'method' in type 'Apple' is not assignable to the same property in base type 'IApple'.
Type '() => Promise<{ a: number; b: string; }>' is not assignable to type '<T = Test>() => Promise<T>'.
Type 'Promise<{ a: number; b: string; }>' is not assignable to type 'Promise<T>'.
Type '{ a: number; b: string; }' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to '{ a: number; b: string; }'.