TypeScript - ts(7053):元素隐式具有 'any' 类型,因为 'string' 类型的表达式不能用于索引
TypeScript - ts(7053) : Element implicitly has an 'any' type because expression of type 'string' can't be used to index
在 TypeScript 中,我这样声明一个接口:
export default interface MyDTO {
readonly num: string;
readonly entitle: string;
readonly trb: string;
readonly ucr: string;
readonly dcr: string;
readonly udm?: string;
readonly ddm?: string;
}
通过函数,我想访问 属性 的值,其名称包含在变量中。
private doSomething(dto: MyDTO, property: string): any {
let label: any;
if (['dcr', 'ddm'].includes(property)) {
label = doSomethingElse(dto[property]);
} else {
label = dto[property];
}
return label;
}
不幸的是,TypeScript 给我以下错误消息:
Element implicitly has an 'any' type because expression of type
'string' can't be used to index type 'MyDTO'. No index signature
with a parameter of type 'string' was found on type
'MyDTO'.ts(7053)
有人有想法吗?
谢谢
这是因为 MyDTO
具有显式命名的属性,但您使用的是通用字符串作为索引,因此 TypeScript 表示它无法保证将任何字符串传递到您的doSomething
函数实际上会匹配您界面上的 属性 名称。
TypeScript 2.1 中引入的一个很好的解决方法是 keyof
。这允许您明确键入某些内容作为某个 class/interface.
的键
这将 A. 消除您看到的 TS 错误,并且 B. 还检查以确保函数的任何调用者实际传递了有效密钥。
export default interface MyDTO {
readonly num: string;
readonly entitle: string;
readonly trb: string;
readonly ucr: string;
readonly dcr: string;
readonly udm?: string;
readonly ddm?: string;
}
function doSomething(dto: MyDTO, property: keyof MyDTO): any {
let label: any;
if (['dcr', 'ddm'].includes(property)) {
label = doSomethingElse(dto[property]);
} else {
label = dto[property];
}
return label;
}
doSomething(obj, "foo") // is a TS error
doSomething(obj, "num") // is valid
@mhodges,根据您的建议,这是我修改后的功能,似乎运行良好。
但是在以下情况下,我必须添加“as string”,否则会出现以下错误:
Type 'string | keyof V 'cannot be used to index type' V'.ts (2536)
public getDefaultComparator(property: keyof V | string, v1: V, v2: V): number {
let compareReturn = 0;
if (v1.hasOwnProperty(property)) {
const compareValue1 = v1[property as string];
const compareValue2 = v2[property as string];
if (compareValue1 > compareValue2) {
compareReturn = 1;
} else if (compareValue1 < compareValue2) {
compareReturn = -1;
}
}
return compareReturn;
}
在tsconfig.json文件中。 设置“严格”:真 --> 假。
这对我有用。
在 TypeScript 中,我这样声明一个接口:
export default interface MyDTO {
readonly num: string;
readonly entitle: string;
readonly trb: string;
readonly ucr: string;
readonly dcr: string;
readonly udm?: string;
readonly ddm?: string;
}
通过函数,我想访问 属性 的值,其名称包含在变量中。
private doSomething(dto: MyDTO, property: string): any {
let label: any;
if (['dcr', 'ddm'].includes(property)) {
label = doSomethingElse(dto[property]);
} else {
label = dto[property];
}
return label;
}
不幸的是,TypeScript 给我以下错误消息:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'MyDTO'. No index signature with a parameter of type 'string' was found on type 'MyDTO'.ts(7053)
有人有想法吗?
谢谢
这是因为 MyDTO
具有显式命名的属性,但您使用的是通用字符串作为索引,因此 TypeScript 表示它无法保证将任何字符串传递到您的doSomething
函数实际上会匹配您界面上的 属性 名称。
TypeScript 2.1 中引入的一个很好的解决方法是 keyof
。这允许您明确键入某些内容作为某个 class/interface.
这将 A. 消除您看到的 TS 错误,并且 B. 还检查以确保函数的任何调用者实际传递了有效密钥。
export default interface MyDTO {
readonly num: string;
readonly entitle: string;
readonly trb: string;
readonly ucr: string;
readonly dcr: string;
readonly udm?: string;
readonly ddm?: string;
}
function doSomething(dto: MyDTO, property: keyof MyDTO): any {
let label: any;
if (['dcr', 'ddm'].includes(property)) {
label = doSomethingElse(dto[property]);
} else {
label = dto[property];
}
return label;
}
doSomething(obj, "foo") // is a TS error
doSomething(obj, "num") // is valid
@mhodges,根据您的建议,这是我修改后的功能,似乎运行良好。 但是在以下情况下,我必须添加“as string”,否则会出现以下错误:
Type 'string | keyof V 'cannot be used to index type' V'.ts (2536)
public getDefaultComparator(property: keyof V | string, v1: V, v2: V): number {
let compareReturn = 0;
if (v1.hasOwnProperty(property)) {
const compareValue1 = v1[property as string];
const compareValue2 = v2[property as string];
if (compareValue1 > compareValue2) {
compareReturn = 1;
} else if (compareValue1 < compareValue2) {
compareReturn = -1;
}
}
return compareReturn;
}
在tsconfig.json文件中。 设置“严格”:真 --> 假。
这对我有用。