我正在寻找一种使用枚举键来定义接口允许的键的方法,如何实现呢?
enum Gender {
male = 'male',
female = 'female'
}
interface Person {
name: string;
gender: string;
}
const people: Person[] = [
{ name: 'Mary', gender: 'female' },
{ name: 'Jerry', gender: 'male' },
{ name: 'Joe', gender: 'male' }
]
interface PeopleGroupedByGender {
// HERE: how to use the enum to describe the [key] value here?
[key: keyof typeof Gender]: Person[]
}
const peopleGroupedByGender: PeopleGroupedByGender = {
[Gender.female]: people,
male: people
}