无法使用数组值从嵌套对象中检索值的函数

huangapple go评论67阅读模式
英文:

Unable to type function to retrieve values from nested objects using array values

问题

I understand your request. Here is the translated code and error message:

// 嵌套对象
const obj = {
  one: {
    two: {
      three: 'xxxx',
    },
    four: {
      five: 5,
    },
    six: [1, 2, 3],
  },
}

// 我想要使用键从上面的嵌套对象中获取一个值。

// 下面的示例在JavaScript中工作,但我无法正确定义TypeScript中的类型。

const result = ['one', 'two', 'four'].reduce(
  (acc, key) => acc[key],
  obj,
)
// undefined

const result = ['one', 'two', 'three'].reduce(
  (acc, key) => acc[key],
  obj,
)
// xxxx

const result = ['one', 'six'].reduce(
  (acc, key) => acc[key],
  obj,
)
// [1, 2, 3]

tslint:

由于类型“string”的表达式不能用于类型为“{ one: { two: { three: string; }; four: { five: number; }; six: number[]; }; }”的对象的索引,因此“element”隐含具有“any”类型。
在类型“{ one: { two: { three: string; }; four: { five: number; }; six: number[]; }; }”上找不到参数类型为“string”的索引签名。ts(7053)

我理解类型检查不起作用,因为数组中的“one”是一个字符串,而reduce中的键是“one”。
然而,我想不出一个解决方案。
请告诉我哪种类型定义会起作用。

如果您需要进一步的帮助,请告诉我。

英文:
// nested object
const obj = {
  one: {
    two: {
      three: 'xxxx',
    },
    four: {
      five: 5,
    },
    six: [1, 2, 3],
  },
}

I want to get a value from the above nested object using key.

The sample below works in javascript, but I can't define the type properly in typescript.

const result = ['one', 'two', 'four'].reduce(
  (acc, key) => acc[key],
  obj,
)
// undefined

const result = ['one', 'two', 'three'].reduce(
  (acc, key) => acc[key],
  obj,
)
// xxxx

const result = ['one', 'six'].reduce(
  (acc, key) => acc[key],
  obj,
)
//  [1, 2, 3]

tslint

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ one: { two: { three: string; }; four: { five: number; }; six: number[]; }; }'.
No index signature with a parameter of type 'string' was found on type '{ one: { two: { three: string; }; four: { five: number; }; six: number[]; }; }'.ts(7053)

I understand that the type checking does not work because "one" in the array is a string and the key from reduce is "one".
However, I can't think of a solution.
Please let me know what type definition would work.

答案1

得分: 1

If you defined a type which is a record with a string key and any value:

定义了一个具有字符串键和任意值的记录类型:

type kv = Record<string, any>;

Then your original object is itself a record with a string key, and either another kv or any value:

然后你的原始对象本身是一个具有字符串键的记录,要么是另一个 `kv` 类型,要么是 `any` 类型的值:

const obj: Record<string, kv | any> = {
one: {
two: {
three: 'xxxx',
},
four: {
five: 5,
},
six: [1, 2, 3],
},
}

Then your warnings go away Live example

然后你的警告消失了。【在线示例链接】

英文:

If you defined a type which is a record with a string key and any value:

type kv = Record&lt;string,any&gt;;

Then your original object is itself a record with a string key, and either another kv or any value:

const obj : Record&lt;string, kv | any&gt; = {
  one: {
    two: {
      three: &#39;xxxx&#39;,
    },
    four: {
      five: 5,
    },
    six: [1, 2, 3],
  },
}

Then your warnings go away Live example

huangapple
  • 本文由 发表于 2023年4月13日 20:45:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定