Mapped types on tuples and arrays
In TypeScript 3.1, mapped object types[1] over tuples and arrays now produce new tuples/arrays, rather than creating a new type where members like push()
, pop()
, and length
are converted.For example:
type MapToPromise<T> = { [K in keyof T]: Promise<T[K]> };
type Coordinate = [number, number]
type PromiseCoordinate = MapToPromise<Coordinate>; // [Promise<number>, Promise<number>]
MapToPromise
takes a type T
, and when that type is a tuple like Coordinate
, only the numeric properties are converted.In [number, number]
, there are two numerically named properties: 0
and 1
.When given a tuple like that, MapToPromise
will create a new tuple where the 0
and 1
properties are Promise
s of the original type.So the resulting type PromiseCoordinate
ends up with the type [Promise<number>, Promise<number>]
.