在 TypeScript 中定义具有多种类型的数组
Defining array with multiple types in TypeScript
我有一个形式的数组:[ 1, "message" ]
。
我如何在 TypeScript 中定义它?
Defining array with multiple types in TypeScript
使用联合类型 (string|number)[]
演示:
const foo: (string|number)[] = [ 1, "message" ];
I have an array of the form: [ 1, "message" ].
如果你确定总是只有两个元素[number, string]
那么你可以将它声明为一个元组:
const foo: [number, string] = [ 1, "message" ];
重要提示
当您想要访问仅在其中一种类型上可用的 属性 时,这不适用于具有不同属性的复杂类型。
如果您将其视为元组(请参阅 language spec 的第 3.3.3 节),则:
var t:[number, string] = [1, "message"]
或
interface NumberStringTuple extends Array<string|number>{0:number; 1:string}
var t:NumberStringTuple = [1, "message"];
我的 TS lint 抱怨其他解决方案,所以对我有用的解决方案是:
item: Array<Type1 | Type2>
如果只有一种,可以使用:
item: Type1[]
我正在使用这个版本:
exampleArr: Array<{ id: number, msg: string}> = [
{ id: 1, msg: 'message'},
{ id: 2, msg: 'message2'}
]
它与其他建议有点相似,但仍然很容易记住。
我已经确定了以下格式来输入可以包含多种类型项目的数组。
Array<ItemType1 | ItemType2 | ItemType3>
这适用于测试和类型保护。 https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types
这种格式不适用于测试或类型保护:
(ItemType1 | ItemType2 | ItemType3)[]
如果您有兴趣获取数字或字符串的数组,您可以定义一个类型,该类型将接受数组
type Tuple = Array<number | string>
const example: Tuple = [1, "message"]
const example2: Tuple = ["message", 1]
如果您需要特定顺序的数组(即数字和字符串)
type Tuple = [number, string]
const example: Tuple = [1, "message"]
const example2: Tuple = ["messsage", 1] // Type 'string' is not assignable to type 'number'.
TypeScript 3.9+ 更新(2020 年 5 月 12 日)
现在,TypeScript 还支持命名元组。这大大增加了代码的可理解性和可维护性。 Check the official TS playground.
所以,现在代替未命名:
const a: [number, string] = [ 1, "message" ];
我们可以加名字:
const b: [id: number, message: string] = [ 1, "message" ];
注意:需要一次把所有的名字都加上,有些名字不能省略,例如:
type tIncorrect = [id: number, string]; // INCORRECT, 2nd element has no name, compile-time error.
type tCorrect = [id: number, msg: string]; // CORRECT, all have a names.
提示:如果不确定最后一个元素的个数,可以这样写:
type t = [msg: string, ...indexes: number];// means first element is a message and there are unknown number of indexes.
如果在一个对象中处理具有多个值类型的数组,这对我有用。
{ [key: string]: number | string }[]
请注意 @basarat will not work with complex types as stated by @seawave23 在评论中接受的答案,当您尝试访问 属性 时,TypeScript 会报错
it won't work with complex types with different properties, when you want to access a property available on only one of the types.
const myarray:(TypeA | TypeB)[];
甚至更好地避免在多个地方进行更改,以防您需要添加其他类型,创建类型
type MyMixedType = TypeA | TypeB;
const myarray: MyMixedType[];
我有一个形式的数组:[ 1, "message" ]
。
我如何在 TypeScript 中定义它?
Defining array with multiple types in TypeScript
使用联合类型 (string|number)[]
演示:
const foo: (string|number)[] = [ 1, "message" ];
I have an array of the form: [ 1, "message" ].
如果你确定总是只有两个元素[number, string]
那么你可以将它声明为一个元组:
const foo: [number, string] = [ 1, "message" ];
重要提示
当您想要访问仅在其中一种类型上可用的 属性 时,这不适用于具有不同属性的复杂类型。
如果您将其视为元组(请参阅 language spec 的第 3.3.3 节),则:
var t:[number, string] = [1, "message"]
或
interface NumberStringTuple extends Array<string|number>{0:number; 1:string}
var t:NumberStringTuple = [1, "message"];
我的 TS lint 抱怨其他解决方案,所以对我有用的解决方案是:
item: Array<Type1 | Type2>
如果只有一种,可以使用:
item: Type1[]
我正在使用这个版本:
exampleArr: Array<{ id: number, msg: string}> = [
{ id: 1, msg: 'message'},
{ id: 2, msg: 'message2'}
]
它与其他建议有点相似,但仍然很容易记住。
我已经确定了以下格式来输入可以包含多种类型项目的数组。
Array<ItemType1 | ItemType2 | ItemType3>
这适用于测试和类型保护。 https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types
这种格式不适用于测试或类型保护:
(ItemType1 | ItemType2 | ItemType3)[]
如果您有兴趣获取数字或字符串的数组,您可以定义一个类型,该类型将接受数组
type Tuple = Array<number | string>
const example: Tuple = [1, "message"]
const example2: Tuple = ["message", 1]
如果您需要特定顺序的数组(即数字和字符串)
type Tuple = [number, string]
const example: Tuple = [1, "message"]
const example2: Tuple = ["messsage", 1] // Type 'string' is not assignable to type 'number'.
TypeScript 3.9+ 更新(2020 年 5 月 12 日)
现在,TypeScript 还支持命名元组。这大大增加了代码的可理解性和可维护性。 Check the official TS playground.
所以,现在代替未命名:
const a: [number, string] = [ 1, "message" ];
我们可以加名字:
const b: [id: number, message: string] = [ 1, "message" ];
注意:需要一次把所有的名字都加上,有些名字不能省略,例如:
type tIncorrect = [id: number, string]; // INCORRECT, 2nd element has no name, compile-time error.
type tCorrect = [id: number, msg: string]; // CORRECT, all have a names.
提示:如果不确定最后一个元素的个数,可以这样写:
type t = [msg: string, ...indexes: number];// means first element is a message and there are unknown number of indexes.
如果在一个对象中处理具有多个值类型的数组,这对我有用。
{ [key: string]: number | string }[]
请注意 @basarat will not work with complex types as stated by @seawave23 在评论中接受的答案,当您尝试访问 属性 时,TypeScript 会报错
it won't work with complex types with different properties, when you want to access a property available on only one of the types.
const myarray:(TypeA | TypeB)[];
甚至更好地避免在多个地方进行更改,以防您需要添加其他类型,创建类型
type MyMixedType = TypeA | TypeB;
const myarray: MyMixedType[];