<aside> 📌 튜플은 각 요소 자리에 타입이 고정된 배열이다.
</aside>
const tuple: [number, boolean, string] = [1, false, '3'];
tuple[0] = 3;
tuple[2] = 5; // Type 'number' is not assignable to type 'string'
tuple[3] = '4'; // Type '"4"' is not assignable to type 'undefined'
tuple.push('5');
<aside> 📌 타입을 정의할 때 readonly를 앞에 붙이자
</aside>
const tuple: readonly [number, boolean, string] = [1, true, '2'];
tuple.push('5'); // Property 'push' does not exist on type 'readonly [number, boolean, string]'
const strNumsBool: [string, ...number[], boolean]
= ['hi', 1, 2, 3, 4, 10, true];
...number[]
는 number 타입이 여러 번 나올 수 있음을 알림
let tuple: [number, boolean?, string?] = [1, true, '2'];
tuple = [3, true];
tuple = [4];
tuple = [5, '6']; // Type 'string' is not assignable to type 'boolean | undefined'
?
는 옵셔널 수식어