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

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

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:

  1. // 嵌套对象
  2. const obj = {
  3. one: {
  4. two: {
  5. three: 'xxxx',
  6. },
  7. four: {
  8. five: 5,
  9. },
  10. six: [1, 2, 3],
  11. },
  12. }
  13. // 我想要使用键从上面的嵌套对象中获取一个值。
  14. // 下面的示例在JavaScript中工作,但我无法正确定义TypeScript中的类型。
  15. const result = ['one', 'two', 'four'].reduce(
  16. (acc, key) => acc[key],
  17. obj,
  18. )
  19. // undefined
  20. const result = ['one', 'two', 'three'].reduce(
  21. (acc, key) => acc[key],
  22. obj,
  23. )
  24. // xxxx
  25. const result = ['one', 'six'].reduce(
  26. (acc, key) => acc[key],
  27. obj,
  28. )
  29. // [1, 2, 3]

tslint:

  1. 由于类型“string”的表达式不能用于类型为“{ one: { two: { three: string; }; four: { five: number; }; six: number[]; }; }”的对象的索引,因此“element”隐含具有“any”类型。
  2. 在类型“{ one: { two: { three: string; }; four: { five: number; }; six: number[]; }; }”上找不到参数类型为“string”的索引签名。ts(7053)
  3. 我理解类型检查不起作用,因为数组中的“one”是一个字符串,而reduce中的键是“one”。
  4. 然而,我想不出一个解决方案。
  5. 请告诉我哪种类型定义会起作用。

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

英文:
  1. // nested object
  2. const obj = {
  3. one: {
  4. two: {
  5. three: 'xxxx',
  6. },
  7. four: {
  8. five: 5,
  9. },
  10. six: [1, 2, 3],
  11. },
  12. }

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.

  1. const result = ['one', 'two', 'four'].reduce(
  2. (acc, key) => acc[key],
  3. obj,
  4. )
  5. // undefined
  6. const result = ['one', 'two', 'three'].reduce(
  7. (acc, key) => acc[key],
  8. obj,
  9. )
  10. // xxxx
  11. const result = ['one', 'six'].reduce(
  12. (acc, key) => acc[key],
  13. obj,
  14. )
  15. //  [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:

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

type kv = Record<string, any>;

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

  1. 然后你的原始对象本身是一个具有字符串键的记录,要么是另一个 `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:

  1. 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:

  1. const obj : Record&lt;string, kv | any&gt; = {
  2. one: {
  3. two: {
  4. three: &#39;xxxx&#39;,
  5. },
  6. four: {
  7. five: 5,
  8. },
  9. six: [1, 2, 3],
  10. },
  11. }

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:

确定