我有一个接口(interface),它应该具有特定枚举键类型的键,但是当我声明该类型时,它会给出此错误:
A mapped type may not declare properties or methods.
这是代码:
enum myEnum {
propOne = 'propOne',
propTwo = 'propTwo'
}
export interface myInterface {
[key in myEnum]: anotherInterface;
}
我也尝试像这样指定类型,但它不起作用并给了我语法错误:export interface myInterface {
[key in keyof typeof myEnum]: anotherInterface;
}
我也尝试使用普通对象而不是枚举,但它给出了同样的错误。
最佳答案
您需要使用 Mapped type ,不是接口(interface):
export type MyInterface = {
[key in myEnum]: AnotherInterface;
}
关于javascript - 映射类型不能声明属性或方法 - TypeScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70652935/