这是正确的:
abstract class X<T extends Record<PropertyKey, any> > {
abstract index(): T
}
class Y extends X<{name:string}> {
index() {
return {name:"Test"}
}
}
但这会导致一个错误:
abstract class X<T extends Record<PropertyKey, any> > {
abstract index(): T
}
class Y extends X {
index():{name:string} {
return {name:"Test"}
}
}
错误:
泛型类型'X'需要1个类型参数.ts(2314)
这可能不是一个问题,但如果类有更泛型呢.
像这样,我需要重新输入EmptyObject
作为泛型,即使它是默认值.
type EmptyObject = Record<PropertyKey, never>
abstract class X<E extends Record<PropertyKey, any> = EmptyObject, T extends Record<PropertyKey, any> = EmptyObject> {
abstract index(data: E): T
}
// I want to just do "extends X" and make typescript infer the rest
class Y extends X<EmptyObject, {name:string}> {
index(data: EmptyObject): {name:string} {
return {name:"Test"}
}
}