定义具有多种类型的数组
我有一个形式的数组: [1,“消息”]
。
如何在打字稿中定义此?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一个形式的数组: [1,“消息”]
。
如何在打字稿中定义此?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
使用联合类型
(string|number)[]
演示:如果您确定始终只有两个元素
[number, string]
,那么您可以将其声明为元组:您甚至可以为元组成员提供有意义的名称,例如
id和<代码>文本:
Use a union type
(string|number)[]
demo:If you are sure that there are always only two elements
[number, string]
then you can declare it as a tuple:And you can even provide meaningful names for the tuple members e.g.
id
andtext
:如果您将其视为元组(请参阅 语言规范),然后:
或
If you're treating it as a tuple (see section 3.3.3 of the language spec), then:
or
我的TS棉布抱怨其他解决方案,因此对我有用的解决方案是:
如果只有一种类型,则可以使用:
My TS lint was complaining about other solutions, so the solution that was working for me was:
if there's only one type, it's fine to use:
TypeScript 3.9+ 更新(2020 年 5 月 12 日)
现在,TypeScript 还支持命名元组。这极大地提高了代码的可理解性和可维护性。 查看官方 TS 游乐场。
所以,现在而不是unnamed:
我们可以添加名称:
注意:您需要一次添加所有名称,不能省略某些名称,例如:
提示:如果您不确定的数量最后一个元素,你可以这样写:
TypeScript 4.x+ Variadic Tuple Types
对于 TS 4.x,最后一个示例必须更改为这个:
类型
number
更改为number[]
。更多信息在这里: https:// /www.typescriptlang.org/docs/handbook/release-notes/typescript-4-0.html#variadic-tuple-types
TypeScript 3.9+ update (May 12, 2020)
Now, TypeScript also supports named tuples. This greatly increases the understandability and maintainability of the code. Check the official TS playground.
So, now instead of unnamed:
We can add names:
Note: you need to add all names at once, you can not omit some names, e.g:
Tip: if you are not sure in the count of the last elements, you can write it like this:
TypeScript 4.x+ Variadic Tuple Types
The last example has to be changed to this one for TS 4.x:
The type
number
is changed tonumber[]
.More info here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-0.html#variadic-tuple-types
我已经以以下格式解决了可以包含多种类型项目的数组。
数组&lt; itemType1 | itemType2 | itemType3&gt;
这与测试和键入后卫非常有用。
这种格式与测试或键入后卫无法正常工作:
(itemType1 | itemType2 | itemType2 | itemType3 | itemType3) /代码>
I've settled on the following format for typing arrays that can have items of multiple types.
Array<ItemType1 | ItemType2 | ItemType3>
This works well with testing and type guards. https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types
This format doesn't work well with testing or type guards:
(ItemType1 | ItemType2 | ItemType3)[]
我正在使用这个版本:
它与其他建议有点相似,但仍然很容易并且很好记。
Im using this version:
It is a little bit similar to the other suggestions but still easy and quite good to remember.
如果您有兴趣获取数字或字符串数组,则可以定义一个类型,该类型将采用以下任一数组:
如果您期望特定顺序的数组(即数字和字符串)
If you are interested in getting an array of either numbers or strings, you could define a type that will take an array of either
If you expect an array of a specific order (i.e. number and a string)
甚至更好地避免在多个位置进行更改,以防您需要添加其他类型,请创建类型
or even better to avoid changes in multiple place in case you need to add another type, create type
如果你执行“as const”,类型将是
好的,因为计算机可以进行精确的类型推断。
if you do "as const" , type will be
It is good because of the exact type inference that computer can possible.
如果处理一个对我有用的对象中具有多种值类型的数组。
If dealing with an array with multiple value types in an object this worked for me.