Javascript 如何在Typescript中打印自定义对象的属性?
我想用myObject[stringProp]语法返回一个自定义对象的prop:value数组。我得到的错误如下:Javascript 如何在Typescript中打印自定义对象的属性?,javascript,typescript,Javascript,Typescript,我想用myObject[stringProp]语法返回一个自定义对象的prop:value数组。我得到的错误如下: 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 'str
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'.
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;
}
这是因为您没有明确告诉TypeScript EmployeeListDataRow可以由字符串索引。您需要修改接口,以告知TypeScript您的接口是字符串可索引的: 导出接口EmployeeListDataRow{//无需扩展对象 [key:string]:number | string | Date;//这是一个索引签名 id:编号; 名称:字符串; 起始日期:日期; 状态:字符串; 经理:字符串; 标题:字符串; 帐户:字符串; 团队:字符串; 比率:数字; totalComp:数字; gm:数字; 息税前利润:数字; 计费:字符串; 电话:字符串; 技能:弦乐; }
这是因为您没有明确告诉TypeScript EmployeeListDataRow可以由字符串索引。您需要修改接口,以告知TypeScript您的接口是字符串可索引的: 导出接口EmployeeListDataRow{//无需扩展对象 [key:string]:number | string | Date;//这是一个索引签名 id:编号; 名称:字符串; 起始日期:日期; 状态:字符串; 经理:字符串; 标题:字符串; 帐户:字符串; 团队:字符串; 比率:数字; totalComp:数字; gm:数字; 息税前利润:数字; 计费:字符串; 电话:字符串; 技能:弦乐; }
尝试删除接口上的“extends”对象,或使用return${prop}:${currentEmployee as EmployeeListDataRow[prop]}\n;共享您的tsconfigTry删除接口上的“extends”对象或使用return${prop}:${currentEmployee as EmployeeListDataRow[prop]}\n;分享你的tsconfig
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;
}