【发布时间】:2021-04-07 02:39:29
【问题描述】:
我正在尝试声明一个类型,它应该只接受扩展另一种类型的对象。
例如:
export interface A extends B {
aProp: string;
}
export interface B {
bProp: string;
// Feels wrong but how to tell something like ReadonlyArray<subClass extends B>?
// I need children to contain A (or anything that extends B) & B properties
children: ReadonlyArray<any & B>;
childrenThatDoesntWork: ReadonlyArray<any extends B>; // <-- error here
}
class SomeClass {
fun(obj: any extends B) { // <-- error '?' expected
}
}
如何做到这一点?
谢谢!
【问题讨论】:
-
any 是你想要绕过类型检查时使用的类型,做
any & T是没用的,它意味着any最后。any extends T也没有任何意义 -
是的,但绕过类型检查是我试图避免的。任何解决方案?
标签: typescript generics types interface extends