제네릭에 사용하는 튜플에 전개 연산자 (spread operator, ...) 를 사용할 수 있게 되었다!
function tail<T extends any[]>(arr: readonly [any, ...T]) {
const [_ignored, ...rest] = arr;
return rest;
}
const myTuple = [1, 2, 3, 4] as const;
const myArray = ["hello", "world"];
// type [2, 3, 4]
const r1 = tail(myTuple);
// type [2, 3, 4, ...string[]]
const r2 = tail([...myTuple, ...myArray] as const);
전개 연산자의 위치가 꼭 마지막이 아니어도 된다.
type Strings = [string, string];
type Numbers = number[]
// [string, string, ...Array<number | boolean>]
type Unbounded = [...Strings, ...Numbers, boolean];
&&, ||, ?? 도 할당 연산자 (=) 와 병합할 수 있다.
let values: string[];
// 이전 버전
(values ?? (values = [])).push("hello");
// 4.0
(values ??= []).push("hello");
catch 절에 바인딩되는 에러 타입은 unknown!
delete 연산자를 사용하려면 연산자의 대상은 any, unknown, never 이거나 옵션 항목이어야 한다.
unknow 타입이라는 것이 있었구나
https://stackoverflow.com/questions/51439843/unknown-vs-any
[..] unknown which is the type-safe counterpart of any. Anything is assignable to unknown, but unknown isn't assignable to anything but itself and any without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type.
let vAny: any = 10; // We can assign anthing to any
let vUnknown: unknown = 10; // We can assign anthing to unknown just like any
let s1: string = vAny; // Any is assigable to anything
let s2: string = vUnknown; // Invalid we can't assign vUnknown to any other type (without an explicit assertion)
vAny.method(); // ok anything goes with any
vUnknown.method(); // not ok, we don't know anything about this variable
메타 프로그래밍?
프로그램이 코드를 수정하도록 하는 프로그래밍, 탬플릿 기반으로 컴파일러에게 '다른 언어의 프로그램 코드를 생성하도록!' 하는 것 - 이라고 한다. 이 개념이 타입스크립트에 등장하는 이유는, 타입스크립트가 컴파일 언어이지만 컴파일 결과가 machine code 가 아닌 JavaScript (=다른 언어의 프로그램 코드) 이기 때문이다. 비슷한 맥락에서, 그렇기 때문에 compile 대신 transpile 이라고도 한다.
https://freshrimpsushi.tistory.com/1457
https://moon9342.github.io/what-is-metaprogramming
(위에 이어서) transpile vs compile
transpiler 은 compiler 의 일종으로 생각된다. transpiler 의 정의가 source to source compiler 니까.