映射类型不能声明属性或方法 - TypeScript

A mapped type may not declare properties or methods - TypeScript

我有一个接口,它应该有特定枚举键类型的键,但是当我声明类型时它给出了这个错误:

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,而不是接口:

export type MyInterface = {
  [key in myEnum]: AnotherInterface;
}