【问题标题】:How to access a field of an object in Typescript using get method?如何使用 get 方法访问 Typescript 中对象的字段?
【发布时间】:2021-08-20 05:35:12
【问题描述】:

在 Typescript 中我定义了一个接口:

interface IPriority {
    KEY1: number;
    KEY2: number;
    KEY3: number;
    DEFAULT: number;
}

我将IPriority 类型的对象传递给函数,如下所示:

    class Foo {
        async someFunction(priority: IPriority) {
        const someMap: Map<string, string> = new Map();
        //someMap has been properly initialized and has valid data
        const key = someMap.get("KEY1") ?? "KEY1";
        //const key = "KEY1";
        if(key) {
          const temp = priority[key];
          console.log(temp);
        }
      }
    }

这样做,我得到这个错误:Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'IPriority'. No index signature with a parameter of type 'string' was found on type 'IPriority'.

如果我将const temp = priority[key]; 更改为此const temp = priority.get(key),我会收到此错误:Property 'get' does not exist on type 'IPriority'.

如何使用 get 方法或对象索引语法 (priority[key]) 访问 KEY1KEY2、... 的值?

这是code的链接

【问题讨论】:

  • priority[key] 有什么问题?
  • @enzo “元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引“IPriority”类型。没有带有“字符串”类型参数的索引签名在类型 'IPriority' 上找到。"
  • @StupidMan that error does not occur with your example code 所以你应该写priority[key] 或更改问题以包含一个真正的minimal reproducible example
  • @StupidMan 你有“元素隐式有一个'任何'类型......”错误,因为你可能没有将键定义为const 键。见this
  • @StupidMan 您的 const 键必须是字符串,而不是数字或未定义。这是编译器所必需的。

标签: typescript typescript2.0


【解决方案1】:

显然编译器需要预定义的索引值。你有两个选择:

  1. 使用 any 这样的关键字 (priority as any)[key]
  2. 创建字典见this

【讨论】:

    【解决方案2】:

    正如Alireza Ahmadi 所说,最好在打字稿中使用字典而不是interfaceMap。但是如果你想坚持你的代码并使用书面代码,你可以通过Object上定义的两个函数找到创建键的值:Object.keysObject.values。您可以将您的 someFunctions 方法更改为以下内容:

    interface IPriority {
      KEY1: string;
      KEY2: string;
      KEY3: string;
    } 
    
    class Foo {
       async someFunction(priority: IPriority) {
          const someMap: Map<string, string> = new Map();
          someMap.set("1", "KEY1");
          someMap.set("2", "KEY2");
          someMap.set("3", "KEY3");
          const key1 = someMap.get("1");
          if (key1) {
             let value: string = "";
             Object.keys(priority).map((propertyKey, propertyIndexKey) => {
               if (propertyKey == key1) {
                  Object.values(priority).map((propertyValue, propertyIndexValue) => {
                     if (propertyIndexKey == propertyIndexValue) {
                        value = propertyValue;
                     }
               })
            }
            return value;
            })
            console.log(value);
         }
      }
    }
    
    const foo: Foo = new Foo();
    const priority: IPriority = {
      KEY1: "1",
      KEY2: "2",
      KEY3: "3",
    }
    foo.someFunction(priority)// "1"
    

    【讨论】:

      猜你喜欢
      • 2021-09-01
      • 2022-12-13
      • 2020-10-22
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 2020-08-13
      • 2013-03-05
      相关资源
      最近更新 更多