我目前正在 React 中制作一个组件,其中一个属性称为“initNavData”。这是一个属性名称总是不同的对象,并且这些属性的数量未知。他们每个人都有相同的结构。我如何为此创建类型?
我看到了 [x: string] 建议,但这是否允许无限数量的属性?
interface WizardProps {
id: string;
// parentWizard: string
isSubwizard: boolean;
wizardName: string;
initNavData: {
[x: string]: {
label: string;
status: string;
current: boolean;
active: boolean;
icon: string;
key: string;
}
}
}
最佳答案
是的,您可以使用索引签名
。作为TypeScript handbook says :
Sometimes you don’t know all the names of a type’s properties ahead of time, but you do know the shape of the values.
In those cases you can use an index signature to describe the types of possible values
关于javascript - typescript :类型是属性名称未知的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73035676/