我有enum HealthPlanStatus生成的enum HealthPlanStatus,最后我想使用枚举的键和值不仅为type IHealthPlanResponse生成 status 键,还生成 title 值作为枚举的值。
export enum HealthPlanStatus {
Todo = 'To-Do',
InProgress = 'Working on it',
}
export type IHealthPlanResponse = {
[status in keyof typeof HealthPlanStatus]: {
title: string;
};
};
它给了我一个严格的结构,我有一个 status 键作为枚举的键(Todo,InProgress...):
type IHealthPlanResponse = {
readonly Todo: {
title: string;
};
readonly InProgress: {
title: string;
};
}
我也希望有一个 title 类型作为枚举的值。例如:
type IHealthPlanResponse = {
readonly Todo: {
title: 'To-Do';
};
readonly InProgress: {
title: 'Working on it';
};
}
2条答案
按热度按时间ep6jt1vc1#
这对你有用吗?
如果你不喜欢在这里看到枚举'key',而想把
string
作为一种类型,你可以把它改为:rn0zuynd2#
在一个类型的值中使用枚举的惊人方法。