extends
-
inferComputer Science/TypeScript 2023. 9. 30. 22:40
infer는 조건부 타입에서만 사용이 가능하다. Conditional types ( 조건부 타입 ) 조건부타입은 TypeScript에서 특정 조건에 따라 타입을 결정하는 기능이다. T extends U ? X : Y 형태를 가지며 이는 타입 T가 타입 U에 할당 가능하면 X 타입을 반환하고, 그렇지 않으면 Y 타입을 반환한다는 뜻이다. type IsString = T extends string ? "yes" : "no"; type Test1 = IsString; // "yes" type Test2 = IsString; // "no" infer 조건부 타입 내에서 동적으로 타입을 추론할 때 사용한다 type ReturnTypeOf = T extends (...args: any[]) => infer R ? ..
-
type vs interfaceComputer Science/TypeScript 2023. 9. 22. 22:57
https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces Documentation - Everyday Types The language primitives. www.typescriptlang.org TypeScript 공식 사이트에서 간략하게 차이를 설명하고 있다. 1. 선언 Interface 와 type 모두 타입을 정의하는 주요 방법이지만, 선언 방식과 정의 할 수 있는 경우의 차이가 존재한다. 둘다 객체와 함수를 정의할 수 있지만 string, number, union, tuple 등 기본 타입은 type 에서만 선언 할 수 있으며 interface에서..