TS 如何声明一个有多种类型的数组

时间:2023-01-23 浏览:158 分类:JavaScript

在Typescript中,可以使用union type 。Union type ,允许将现有的类型和数据存储从一个联盟类型中组合起来。

Union type 用管道(|)符号表示数字和字符串的(数字|字符串)

例如:

let words:(number|string)[]=["one",1,"two","three","four"];
let words1:Array=["one",1,"two",2,"three","four"];