📎아이템 50 오버로딩 타입보다는 조건부 타입을 사용하기
function double(x) {
return x + x;
}function double(x: number|string): number|string;
function double(x: any) { return x + x; }const num = double(12); // string | number
const str = double('x'); // string | number🔗 오버로딩 개선하기
⭐️ 제너릭을 사용하기
function double<T extends number | string>(x: T): T;
function double(x: any) {
return x + x;
}
const num = double(12); // 타입이 12
const str = double('x'); // 타입이 "x"⭐️ 여러 가지 타입 선언으로 분리하기
⭐️ 조건부 타입(conditional type)을 사용하기
📍 정리
📍 요약
Last updated