【发布时间】:2020-04-19 05:03:45
【问题描述】:
我正在尝试使用联合类型键创建松散类型对象的接口。
export type ObjectsType = 'text' | 'image' | 'circleText';
export interface IAllowedObjects {
[key: ObjectsType] : boolean;
}
但得到
索引签名参数类型不能是联合类型。考虑 改为使用映射对象类型
已经尝试了一些解决方案,但没有成功。
- 试过This解决方案
export type ObjectsType = 'text' | 'image' | 'circleText';
export interface IAllowedObjects {
[key in ObjectsType] : boolean;
}
接口中的计算属性名称必须引用表达式 其类型是文字类型或“唯一符号”
计算属性名称的类型必须为“字符串”、“数字”、“符号”、 或“任何”。
【问题讨论】:
标签: typescript interface typescript-typings