简体   繁体   English

将类型分配给动态访问的 object 值

[英]Assigining type to dynamically accessed object values

I have created a class which has 2 properties storing arrays of tuples with numbers.我创建了一个 class ,它有 2 个属性存储 arrays 元组和数字。 I also have created a method "getIndex" that accesses those properties dynamically and then checks whether there is a tuple containing identical numbers.我还创建了一个方法“getIndex”,它动态访问这些属性,然后检查是否存在包含相同数字的元组。 The function looks like this: function 看起来像这样:

  getIndex(posX: number, posY: number, pathArr: string) {
    for (let routeCoordinates of this[pathArr]) {
      if (routeCoordinates[0] === posX && routeCoordinates[1] === posY) {
        return this[pathArr].indexOf(routeCoordinates);
      }
    }
  }

The problem is that whenever I try to pass pathArr as a key I get the following error message:问题是,每当我尝试将 pathArr 作为键传递时,都会收到以下错误消息:

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

I know about generics but solutions I found on the internet do not work as I am dealing with a class instance.我知道 generics 但我在互联网上找到的解决方案不起作用,因为我正在处理 class 实例。 Is there a solution that keeps typechecking?有没有保持类型检查的解决方案?

The immediate solution is pretty simple.直接的解决方案非常简单。 All you need to do is change pathArr: string parameter to pathArr: keyof PathfinderSecondStage which will cause TypeScript to create a union type of all the public fields/methods within the class and then extract the this[pathArr] statement and assign it into a variable with the assistance of type assertion to tell it the exact type that is going to be used:您需要做的就是将pathArr: string参数更改为pathArr: keyof PathfinderSecondStage这将导致 TypeScript 创建 class 中所有公共字段/方法的联合类型,然后提取this[pathArr]语句并将其分配给变量在类型断言的帮助下告诉它要使用的确切类型:

 getIndex(posX: number, posY: number, pathArr: keyof PathfinderSecondStage) {
    const myArray = this[pathArr] as number[][];
    for (let routeCoordinates of myArray) {
      if (routeCoordinates[0] === posX && routeCoordinates[1] === posY) {
        return myArray.indexOf(routeCoordinates);
      }
    }
  }

You can additionally narrow down the allowed variables for the pathArr parameter by specifically defining the names of the fields that contain the arrays which can be traversed within the function as below: getIndex(posX: number, posY: number, pathArr: 'fieldName1'|'fieldName2') {您还可以通过具体定义包含 arrays 的字段的名称来缩小pathArr参数的允许变量,这些字段可以在 function 中遍历,如下所示: getIndex(posX: number, posY: number, pathArr: 'fieldName1'|'fieldName2') {

This will narrow down the number of types TypeScript needs to create a union type with, and if they are all arrays of numbers ( number[][] ), you will not need to do the type assertion as displayed in the first example.这将缩小类型 TypeScript 需要创建联合类型的数量,如果它们都是数字的 arrays ( number[][] ),则不需要执行第一个示例中显示的类型断言。

Without the full code it is impossible to know what this[] refers to, but you can allow a Typescript array to be indexed with strings by declaring it as let ARRAY: { [key: string]: DTYPE; } = {};如果没有完整的代码,就不可能知道this[]指的是什么,但是您可以通过将 Typescript 数组声明为let ARRAY: { [key: string]: DTYPE; } = {};来允许用字符串索引它。 let ARRAY: { [key: string]: DTYPE; } = {}; where ARRAY is your array name and DTYPE is the data type you are storing.其中 ARRAY 是您的数组名称,DTYPE 是您要存储的数据类型。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM