TypeScript 入門 Push型を定義する

配列に要素を追加するような型を定義します。

type Push<T extends unknown[], U> = [...T,U]

使い方。

type T1 = Push<[1, 2], 3> //[1, 2, 3]
type T2 = Push<[1, 2], '3'> //[1, 2, '3']
type T3 = Push<[], 1> //[1]