【发布时间】:2022-01-22 03:27:14
【问题描述】:
目标
我想要一个描述具有以下类型值的对象的类型:Array<ClassRef> 其中ClassRef 是任何必须实现某个接口的类。
方法
type KeyClassMap<SomeUnionType> = {
[key in keyof SomeUnionType]: Array<ClassRef>
}
type ClassRef = new (...args: any[]) => any // should extend that interface
interface I {
[Symbol.hasInstance](hint: string): boolean
}
const _map: KeyClassMap<UnionType> = {
myKey: [Date, MyOwnClass, MyOtherClass] // <- all these should have implemented Symbol.hasInstance to check with `instanceof` operator
}
问题
该示例缺少实现该接口的类的声明。我不知道该怎么做。
每个类只应实现这一方法 ([Symbol.hasInstance]) 以检查值是否是该类的实例。
我想知道如何声明实现该接口的类的类型或任何其他方式来描述具有此方法的类。
我认为这是不可能的,所以我很感激任何其他提示。
【问题讨论】:
-
_map是否可能是KeyClassMap<Whatever>类型?如果不是,我不太确定你的意思。 -
哦,是的,谢谢@Grochni!
标签: javascript typescript