我已经创建了一个对象并为其定义了一个接口。 尝试使用方括号表示法更新属性时,打字稿说我的对象没有索引签名,即使我定义了索引签名也是如此。
我的对象
const updatedState:internalStateShape = {
...this.state,
customerDetailsForm: {
...this.state.customerDetailsForm,
formFields: {
...this.state.customerDetailsForm.formFields,
[fieldId]: {
...this.state.customerDetailsForm.formFields[fieldId]
}
}
}
};
我的internalStateShape接口
interface internalStateShape {
currentCustomerKey: string,
customerDetailsFormMode: string,
customerModalOpenState: boolean,
searchModalOpenState: boolean,
searchTerm: string,
customerDetailsForm: {
formFields: {
[key:string]: formFieldShape
},
formValid: boolean,
},
searchFieldForm: {
formFields: {
[key:string]: formFieldShape
},
formValid: boolean,
}
};
我的formFieldShape接口
interface formFieldShape {
classes: string
config: {
id: string,
type: string,
placeholder: string,
label: string
},
value: string,
validation: {
required: boolean,
regex: RegExp,
message: string,
},
valid: boolean,
touched: boolean,
showError: boolean
};
在internalStateShape.customerDetailsForm.formFields中,我已指定[key:string]作为索引。
Typescript抱怨
Element implicitly has an 'any' type because type '{ "firstName": { classes: string; config: { id: string; type: string; placeholder: string; label: string; }; value: string; validation: { required: boolean; regex: RegExp; message: string; }; valid: boolean; touched: boolean; showError: boolean; }; "lastName": { ...; }; "dateOfBirth": { ...; }; }' has no index signature.ts(7017)
到达时
...this.state.customerDetailsForm.formFields[fieldId]
在我的updatedState对象中
为什么会这样?