TypeScript の 可変長タプル型
レベル 5447 打鍵
タプル型の中のスプレッド構文で、Concat、Push、Unshift を型レベルで作ります。各値は結果のタプルに要素ごとに正確に一致する必要があります。
TypeScript のコード
// T の要素の後に U の要素を並べる1type Concat<T extends unknown[], U extends unknown[]> = [...T, ...U];// 末尾に V を加える。型版の array.push2type Push<T extends unknown[], V> = [...T, V];// 先頭に V を加える。array.unshift と同じ3type Unshift<T extends unknown[], V> = [V, ...T];// 数値リテラル型のタプル4type A = [1, 2];5type B = [3, 4];// [1, 2, 3, 4] になる6type AB = Concat<A, B>;// [1, 2, 5] になる7type Pushed = Push<A, 5>;// [0, 3, 4] になる8type Unshifted = Unshift<B, 0>;// 値は正確に一致する必要がある。[1, 2, 3, 5] は型エラーになる9const ab: AB = [1, 2, 3, 4];10const pushed: Pushed = [1, 2, 5];11const unshifted: Unshifted = [0, 3, 4];// [ 1, 2, 3, 4 ] を表示し、続いて残りの2つのタプルを表示する12console.log(ab);13console.log(pushed);14console.log(unshifted);実行結果
[ 1, 2, 3, 4 ] [ 1, 2, 5 ] [ 0, 3, 4 ]
このコードでタイピング練習ができます。記録はランキングに反映されません。
このコードで使う記号
この TypeScript のコードで打つ記号と、出てくる回数です。
,× 18.× 15;× 14[× 12]× 12=× 11<× 6>× 6:× 3(× 3