简体   繁体   English

Typescript for in循环,遍历object或数组的联合类型

[英]Typescript for in loop, iterate through union type of object or array

I'm trying to use the for in loop over union type of either object or array, since arrays are objects as well and their key property is the index.我试图在 object 或数组的联合类型上使用for in loop ,因为 arrays 也是对象,它们的关键属性是索引。

const loopOver = (question: any[] | {[key: string]: any} ) => {
  for (let key in question) { // error ts 7053
    console.log(question[key])
  }
};

results in error导致错误

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

but once I remove the union type and leave it to either object or array, then I don't get any errors.但是一旦我删除联合类型并将其留给 object 或数组,我就不会收到任何错误。

// okay
const loopOver = (question: {[key: string]: any} ) => {
  for (let key in question) {
    console.log(question[key])
  }
};
// okay
const loopOver = (question: any[] ) => {
  for (let key in question) { 
    console.log(question[key])
  }
};

tsconfig tsconfig

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "exclude": ["node_modules"],
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}

ts version in package.json ^3.8.3 package.json 中的 ts 版本^3.8.3

Arrays should be indexed with numbers , not strings. Arrays 应该用数字索引,而不是字符串。 But for..in iterates strings - your key is a string.但是for..in迭代字符串- 你的key是一个字符串。 When looking up properties on the array, cast the string key to a number for it to work.在数组上查找属性时,将字符串键转换为数字以使其工作。 (But when looking up the property on the object, since the object uses [key: string]: any , you have to keep looking up using the string) (但在 object 上查找属性时,由于 object 使用[key: string]: any ,因此您必须继续使用字符串查找)

const loopOver = (question: [] | {[key: string]: any} ) => {
  for (let key in question) {
    if (Array.isArray(question)) {
      console.log(question[Number(key)])
    } else {
      console.log(question[key])
    }
  }
};

But, from this code, it looks like you don't actually care about the keys at all (and for..in should not be used with arrays anyway).但是,从这段代码来看,您似乎根本不关心密钥(无论如何, for..in 都不应该与 arrays 一起使用)。 You only care about the values, so how about using Object.values instead?您只关心值,那么使用Object.values怎么样?

const loopOver = (question: [] | {[key: string]: any} ) => {
  for (const val of Object.values(question)) {
    console.log(val)
  }
};

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

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