我正在尝试利用 Record
创建一个可重复使用的类型类型。
enum MyKeys {
ALPHA = 'ALPHA',
BETA = 'BETA',
GAMMA = 'GAMMA',
}
interface MyValues {
in: any[];
out: any[];
}
type Case<T> = Record<T, MyValues>;
理想情况下我可以使用 Case<MyKeys>
而不是 Record<MyKeys, MyValues>
.
Type 'T' does not satisfy the constraint 'string | number | symbol'.
Type 'T' is not assignable to type 'symbol'
最佳答案
类型参数 T
需要限制为有效的索引类型:
type Case<T extends string> = Record<T, MyValues>;
关于typescript - 创建一个采用枚举参数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53820177/