我正在为我的项目使用Typescript和React,我得到了这个错误:
const dataKey: String
Type 'String' cannot be used as an index type. ts(2538)
下面是我的代码(简化):
export interface UserData {
email: string;
name: string;
buyer: boolean;
}
createObject(data: any) {
const dataObj: UserData = {
email: '',
name: '',
buyer: '',
};
for (const i in data) {
const dataKey: String = data[i].field.toLowerCase();
dataObj[dataKey] = data[i].value;
}
return dataObj;
}
我不知道如何在不使用字符串值的情况下将键/值对添加到我的dataObj中。我必须在它们的dataKey上执行小写字符串操作。
有人知道我该如何修改这段代码吗?
转载请注明出处:http://www.smbzepe.com/article/20230330/1401283.html