【发布时间】:2021-03-16 18:42:52
【问题描述】:
我想像这样在 TypeScript 中定义接口,但我不知道该怎么做:
export interface SomeInterface {
testProp:{
[key: string]: {
prop1: string;
prop2?: string;
prop3?: string;
....
};
}
requiredProps: ???? // <- here i would like to define type to be array of used keys
}
例如对象:
const value = {
testProp: {
orange: {
prop1: 'test1',
},
kiwi: {
prop1: 'random text',
},
lemon: {
prop1: 'text',
},
},
requiredProps: [] // posible items in array "lemon", "kiwi", "orange"
};
我尝试将 requiredProps 定义为 requiredProps: [keyof Pick<SomeInterface,"testProp"] 但没有成功
【问题讨论】:
标签: typescript typescript-typings typescript2.0 typescript-generics