简体   繁体   English

有没有一种方法可以编写用于在TypeScript中解构对象数组的类型?

[英]Is there a way to write types for destructuring array of objects in TypeScript?

I need to write typings for a structure like this one: 我需要为这种结构编写类型:

const [{ start }] = myObjectsArray;

However I cannot write it as just a string, or an object like this one: 但是我不能只将它写为字符串或类似这样的对象:

const [{ start }]: { start: string } = myObjectsArray;

Because in TypeScript I get an error, saying that it is no an array type. 因为在TypeScript中我得到一个错误,说它不是数组类型。 When I try to type something like 当我尝试输入类似

const [{ start }]: [{ start: string }] = myObjectsArray;

I get an error, that the structure is missing the property '0'. 我收到一个错误,该结构缺少属性“ 0”。

What is the way to correctly write types for a structure like this one? 为这种结构正确编写类型的方法是什么?

It looks like I'd found the answer while I was writing the question, so I'm posting it here. 看来我在写问题时找到了答案,所以我将其发布在这里。

The way that turns out to be working in such cases is the next one: 在这种情况下起作用的方式是下一个:

const [{ start }]: Array<{ start: string }> = myObjectsArray;

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

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