【发布时间】:2019-11-07 00:49:44
【问题描述】:
我正在尝试扩展抽象泛型类,但在扩展某些方法时遇到了问题。
考虑一下:
abstract class A<T,K> {
protected abstract upload<S>(item: T): S
protected abstract download(item: T): K
}
class B<T, K > extends A<T, K>{
protected upload(item: T):string {
return 'hello'
}
protected download(item: T): number{
return 1
}
}
B 类中的 upload 方法的错误是:
Property 'upload' in type 'B<T, K>' is not assignable to the same property in base type 'A<T, K>'.
Type '(item: T) => string' is not assignable to type '<S>(item: T) => S'.
Type 'string' is not assignable to type 'S'.
对于download 类中的B 方法:
Property 'download' in type 'B<T, K>' is not assignable to the same property in base type 'A<T, K>'.
Type '(item: T) => number' is not assignable to type '(item: T) => K'.
Type 'number' is not assignable to type 'K'.
'number' is assignable to the constraint of type 'K', but 'K' could be instantiated with a different subtype of constraint '{}'.
【问题讨论】:
标签: typescript typescript-generics