Defining array with multiple types in TypeScript

ArraysTypescript

Arrays Problem Overview


I have an array of the form: [ 1, "message" ].

How would I define this in TypeScript?

Arrays Solutions


Solution 1 - Arrays

> Defining array with multiple types in TypeScript

Use a union type (string|number)[] demo:

const foo: (string|number)[] = [ 1, "message" ];

> I have an array of the form: [ 1, "message" ].

If you are sure that there are always only two elements [number, string] then you can declare it as a tuple:

const foo: [number, string] = [ 1, "message" ];

IMPORTANT NOTE

This won't work with complex types with different properties, when you want to access a property available on only one of the types.

See this newer answer.

Solution 2 - Arrays

If you're treating it as a tuple (see section 3.3.3 of the language spec), then:

var t:[number, string] = [1, "message"]

or

interface NumberStringTuple extends Array<string|number>{0:number; 1:string}
var t:NumberStringTuple = [1, "message"];

Solution 3 - Arrays

My TS lint was complaining about other solutions, so the solution that was working for me was:

item: Array<Type1 | Type2>

if there's only one type, it's fine to use:

item: Type1[]

Solution 4 - Arrays

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:

const a: [number, string] = [ 1, "message" ];

We can add names:

const b: [id: number, message: string] = [ 1, "message" ];

Note: you need to add all names at once, you can not omit some names, e.g:

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.

Tip: if you are not sure in the count of the last elements, you can write it like this:

type t = [msg: string, ...indexes: number];// means first element is a message and there are unknown number of indexes.

Solution 5 - Arrays

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)[]

Solution 6 - Arrays

Im using this version:

exampleArr: Array<{ id: number, msg: string}> = [
   { id: 1, msg: 'message'},
   { id: 2, msg: 'message2'}
 ]

It is a little bit similar to the other suggestions but still easy and quite good to remember.

Solution 7 - Arrays

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

type Tuple = Array<number | string>
const example: Tuple = [1, "message"]
const example2: Tuple = ["message", 1]

If you expect an array of a specific order (i.e. number and a string)

type Tuple = [number, string]
const example: Tuple = [1, "message"]
const example2: Tuple = ["messsage", 1] // Type 'string' is not assignable to type 'number'.

Solution 8 - Arrays

If dealing with an array with multiple value types in an object this worked for me.

 { [key: string]: number | string }[]

Solution 9 - Arrays

Please note that the accepted answer by @basarat will not work with complex types as stated by @seawave23 in the comments, when you try to access a property, TypeScript will complain

> it won't work with complex types with different properties, when you want to access a property available on only one of the types.

Solution 10 - Arrays

const myarray:(TypeA | TypeB)[];

or even better to avoid changes in multiple place in case you need to add another type, create type

type MyMixedType = TypeA | TypeB;
const myarray: MyMixedType[];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondk123View Question on Stackoverflow
Solution 1 - ArraysbasaratView Answer on Stackoverflow
Solution 2 - ArrayscurpaView Answer on Stackoverflow
Solution 3 - ArraysSzamanmView Answer on Stackoverflow
Solution 4 - ArraysOleg ZarevennyiView Answer on Stackoverflow
Solution 5 - Arraysj4ys0nView Answer on Stackoverflow
Solution 6 - ArraysDerKarimView Answer on Stackoverflow
Solution 7 - ArraysCaliTabView Answer on Stackoverflow
Solution 8 - ArraysJerehme GayleView Answer on Stackoverflow
Solution 9 - ArraysA. KhaledView Answer on Stackoverflow
Solution 10 - ArraysTech WizardView Answer on Stackoverflow