简体   繁体   English

Object.keys - 元素在 Typescript 中隐式具有类型“any”

[英]Object.keys - Element has type "any" implicitly in Typescript

I have a function to obtain the key having the value of the property.我有一个 function 来获取具有属性值的键。 When replicating this function in Typescript I get the following error:在 Typescript 中复制此 function 时出现以下错误:

The element implicitly has type "any" because the expression of type "any" cannot be used to index the type "TestModel"

This is my function:这是我的 function:

interface TestModel {
    test: string,
    "test 1": string,
    data: string,
    "data 1": string
  }

getKeyByValue(value: string) {

        let data: TestModel = {
            test: "test",
            "test 1": "one Test",
            data: "data",
            "data 1": "one data"
        }

        return Object.keys(data).find((key: any) => data[key] === value);
    }

UPDATE更新

New function:新 function:

return Object.keys(data).find((key: string) => data[key] === value);

Error:错误:

let data: TestModel
The element implicitly has type "any" because the expression of type "string" cannot be used to index the type "TestModel".
   No index signature found with a parameter of type "string" on type "TestModel"

There is no need to use explicit any type for key.无需为密钥使用显式any类型。 TS is able to figure out that type of key is string. TS 能够判断出该类型的key是字符串。 However, we know that type of key is keyof TestModel .但是,我们知道key的类型是keyof TestModel

Object.keys always returns string[] instead of keyof T - this is by design, for safety reasons. Object.keys总是返回string[]而不是keyof T - 这是设计使然,出于安全原因。 Hence the most common way in typescript is to use type assertion in this case:因此 typescript 中最常见的方法是在这种情况下使用type assertion

interface TestModel {
    test: string,
    "test 1": string,
    data: string,
    "data 1": string
}

class Foo  {

    getKeyByValue(value: string) {

        let data: TestModel = {
            test: "test",
            "test 1": "one Test",
            data: "data",
            "data 1": "one data"
        }

        return (
            (Object.keys(data) as Array<keyof TestModel>)
                .find((key) => data[key] === value)
        );
    }
}

You can find more information in my article您可以在我的文章中找到更多信息

暂无
暂无

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

相关问题 Object.keys 迭代导致 Typescript 错误“元素隐式具有 'any' 类型,因为索引表达式不是类型 'number'” - Object.keys iteration causing Typescript error “Element implicitly has an 'any' type because index expression is not of type 'number'” TS 7015:元素隐式具有“任何”类型,因为索引表达式不是 Object 的“数字”类型。getElementsByTagName 的键 - TS 7015: Element implicitly has an 'any' type because index expression is not of type 'number' for Object.keys of getElementsByTagName 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于在 Object.keys 中索引“ - Element implicitly has an 'any' type because expression of type 'string' can't be used to index' within Object.keys Typescript - 使用键获取 object 值 - 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型 - Typescript - Getting object values with keys - Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 带有字符串键的嵌套 object 上的 typescript 错误:TS7015:元素隐式具有“任何”类型,因为索引表达式不是“数字”类型 - typescript error on nested object with string keys: TS7015: Element implicitly has an 'any' type because index expression is not of type 'number' 在Typescript中使用Object.keys时,避免使用隐式“any”类型 - Avoid implicit 'any' type when using Object.keys in Typescript TypeScript:元素隐含地具有 RegExp 的“任何”类型 - TypeScript: Element implicitly has an 'any' type for RegExp 打字稿:可能导致此错误的原因是什么? “元素隐式具有&#39;任意&#39;类型,因为类型&#39;对象&#39;没有索引签名” - Typescript: what could be causing this error? “Element implicitly has an 'any' type because type 'Object' has no index signature” 在打字稿中输入 Object.keys(someEnum) - Type for Object.keys(someEnum) in typescript 键入一个对象的问题反应typescript - Element隐式有一个&#39;any&#39;类型因为type&#39; - issue typing an object react typescript - Element implicitly has an 'any' type because type'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM