简体   繁体   English

如何在 TypeScript 的 for..of 循环中声明和解构元组数组?

[英]How to Declare and Destructure a Tuple Array in a for..of Loop in TypeScript?

I have the following JavaScript code in a test file:我在测试文件中有以下 JavaScript 代码:

for (let [input, expected] of [
  ['<h1>test</h1>', ['</h1>']],
  ['<h1><b>test</b></h1>', ['</h1>', '</b>']],
  ['<h1><b>test</b> lol</h1>', ['</h1>']],
]) {
  const result = functionToTest(input);
  for (let i in expected) {
    if (result[i] !== expected[i]) {
      throw Error("test failed")
    }
  }
}

TypeScript does not accept this code because functionToTest expects a string and TypeScript thinks that input has type string | string[] TypeScript 不接受此代码,因为functionToTest需要一个string而 TypeScript 认为input类型为string | string[] string | string[] . string | string[]

One thing that works is to put my test data in a separate variable and declare its type as a list of tuples:可行的一件事是将我的测试数据放在一个单独的变量中,并将其类型声明为元组列表:

const testData: [string, string[]][] = [
  ['<h1>test</h1>', ['</h1>']],
  ['<h1><b>test</b></h1>', ['</h1>', '</b>']],
  ['<h1><b>test</b> lol</h1>', ['</h1>']],
]

for (let [input, expected] of testData) {
  const result = listClosingTagsAtEnd(input);
  for (let i in expected) {
    if (result[i] !== expected[i]) {
      throw Error("test failed")
    }
  }
}

However I would like not to have to create a variable to store data that I only need once.不过,我想不会有创建一个变量来存储数据,我只需要一次。 I liked it better when it was declared in the for loop.当它在 for 循环中声明时,我更喜欢它。

I tried to do type declaration in the for loop with let [input, expected]: [string, string[]] of but I get the error The left-hand side of a 'for...of' statement cannot use a type annotation.我尝试在 for 循环中使用let [input, expected]: [string, string[]] of进行类型声明let [input, expected]: [string, string[]] of但出现错误The left-hand side of a 'for...of' statement cannot use a type annotation. . .

I also tried to add : [string, string[]] after the declaration of the test data in the for loop but then TypeScript thinks I am using a JavaScript label .我还尝试在 for 循环中的测试数据声明之后添加: [string, string[]]但随后 TypeScript 认为我使用的是JavaScript label

How can I have properly typed destructuration while keeping the declaration of test data inside the for loop?如何在将测试数据的声明保留在 for 循环中的同时正确键入解构?

I figured this out myself by chance, just “trying random things”, so I wanted to post it to Stack Overflow in case other people have the same problem.我自己偶然发现了这一点,只是“尝试随机的事情”,所以我想把它发布到 Stack Overflow,以防其他人有同样的问题。 Here is how you do it:这是你如何做到的:

for (let [input, expected] of <[string, string[]][]>[
  //                          ^--------------------^
  //                             type declaration
  ['<h1>test</h1>', ['</h1>']],
  ['<h1><b>test</b></h1>', ['</h1>', '</b>']],
  ['<h1><b>test</b> lol</h1>', ['</h1>']],
]) {
  const result = functionToTest(input);
  for (let i in expected) {
    if (result[i] !== expected[i]) {
      throw Error("test failed")
    }
  }
}

Edit: Wow, found another solution on this stack overflow answer !编辑:哇,在这个堆栈溢出答案上找到了另一个解决方案! Just add as const after the literal:只需在文字后添加as const

for (let [input, expected] of [
  ['<h1>test</h1>', ['</h1>']],
  ['<h1><b>test</b></h1>', ['</h1>', '</b>']],
  ['<h1><b>test</b> lol</h1>', ['</h1>']],
] as const) {
//   ^---^
//    HERE
  const result = functionToTest(input);
  for (let i in expected) {
    if (result[i] !== expected[i]) {
      throw Error("test failed")
    }
  }
}

In this case, TypeScript considers that input has type "<h1>test</h1>" | "<h1><b>test</b></h1>" | "<h1><b>test</b> lol</h1>"在这种情况下,TypeScript 认为input类型为"<h1>test</h1>" | "<h1><b>test</b></h1>" | "<h1><b>test</b> lol</h1>" "<h1>test</h1>" | "<h1><b>test</b></h1>" | "<h1><b>test</b> lol</h1>" "<h1>test</h1>" | "<h1><b>test</b></h1>" | "<h1><b>test</b> lol</h1>" for instance, which is ... the most exact description of input 's type indeed. "<h1>test</h1>" | "<h1><b>test</b></h1>" | "<h1><b>test</b> lol</h1>" ,这确实是对input类型最准确的描述。

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

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