我想prop: value
用myObject[stringProp]
语法返回一个自定义对象的数组。我得到这样的错误:
TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{} | EmployeeListDataRow'.
No index signature with a parameter of type 'string' was found on type '{} | EmployeeListDataRow'.
<h1>
{Object.keys(currentEmployee).map((prop) => {
return `${prop}: ${currentEmployee[prop]}\n`;
})}
</h1>
currentEmployee
具有这样的界面:
export interface EmployeeListDataRow extends Object {
id: number;
name: string;
startDate: Date;
status: string;
manager: string;
title: string;
account: string;
team: string;
rate: number;
totalComp: number;
gm: number;
ebit: number;
billable: string;
phone: string;
skills: string;
}
这是因为您没有明确告诉TypeScriptEmployeeListDataRow
可以由字符串索引。您需要修改接口以告诉TypeScript您的接口是可字符串索引的:
export interface EmployeeListDataRow { // no need to extend Object
[key: string]: number | string | Date; // this is an index signature
id: number;
name: string;
startDate: Date;
status: string;
manager: string;
title: string;
account: string;
team: string;
rate: number;
totalComp: number;
gm: number;
ebit: number;
billable: string;
phone: string;
skills: string;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句