简体   繁体   English

TypeScript - ts(7053):元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引

[英]TypeScript - ts(7053) : Element implicitly has an 'any' type because expression of type 'string' can't be used to index

In TypeScript, I declare an interface like this:在 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;
}

With a function, I would like to access the value of a property, whose name is contained in a variable.使用 function,我想访问属性的值,其名称包含在变量中。

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;
}

Unfortunately, TypeScript gives me the following error message:不幸的是,TypeScript 给了我以下错误信息:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'MyDTO'.元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“MyDTO”。 No index signature with a parameter of type 'string' was found on type 'MyDTO'.ts(7053)在类型“MyDTO”.ts(7053) 上找不到带有“字符串”类型参数的索引签名

Anyone have an idea, please?请问有人有什么想法吗?

Thank you谢谢

The reason for this is because MyDTO has explicitly named properties, but you're using a generic string as an index, so TypeScript is saying that it can't guarantee that whatever string is passed into your doSomething function will actually match a property name on your interface.这样做的原因是因为MyDTO已明确命名属性,但您使用通用字符串作为索引,因此 TypeScript 表示它不能保证传递给您的任何字符串doSomething function 实际上会匹配你的界面。

An excellent workaround for this that was introduced in TypeScript 2.1 is keyof .在 TypeScript 2.1 中引入的一个很好的解决方法是keyof This allows you to explicitly type something as a key of a certain class/interface.这使您可以显式键入某些内容作为某个类/接口的键。

This will A. get rid of the TS error you're seeing, and B. also check to make sure that any callers of your function actually pass a valid key.这将 A. 摆脱您看到的 TS 错误,并且 B. 还要检查以确保您的 function 的任何调用者实际上传递了一个有效的密钥。

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, with your suggestions, here is my modified function which seems to work well. @mhodges,根据您的建议,这是我修改后的 function 似乎效果很好。 However in the following case, I had to add the "as string" otherwise I have the following error:但是在以下情况下,我必须添加“作为字符串”,否则会出现以下错误:

Type 'string |键入'字符串 | keyof V 'cannot be used to index type' V'.ts (2536) keyof V '不能用于索引类型' 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;
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TS7053:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{ A:数字; B:数字 - TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ A: number; B: number 错误 TS7053:元素隐式具有“任何”类型,因为“数据”类型的表达式不能用于索引类型“{}” - error TS7053: Element implicitly has an 'any' type because expression of type '“data”' can't be used to index type '{}' 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 React Typescript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript TypeScript - 元素隐式具有“任意”类型,因为“字符串”类型的表达式不能用于索引类型 - TypeScript - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type TypeScript 错误:“元素隐式具有 'any' 类型,因为类型 'any' 的表达式不能用于索引类型 - TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type TS | 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引“记录”类型<SecurityMode, boolean> &#39; - TS | Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Record<SecurityMode, boolean>' Typescript “元素隐式具有‘any’类型,因为类型‘Rank’的表达式不能用于索引类型‘IPowerCards’” - Typescript "Element implicitly has an 'any' type because expression of type 'Rank' can't be used to index type 'IPowerCards'" Typescript 元素隐式具有“任何”类型,因为“数字”类型的表达式不能用于索引 - Typescript Element implicitly has an 'any' type because expression of type 'number' can't be used to index 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“BGColors” - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'BGColors' 如何修复 Element 隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型? - how fix Element implicitly has an 'any' type because expression of type 'string' can't be used to index type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM