[TS] 유틸리티 타입2 - 조건부 타입 기반

이유림·2025년 2월 3일
0

타입스크립트

목록 보기
5/5
post-thumbnail

Exclude<T, U>

Exclude 타입은 다음과 같이 T로부터 U를 제거하는 타입이다.

type A = Exclude<string | boolean, string>;
// boolean

Extract<T, U>

Extract 타입은 다음과 같이 T로 부터 U를 추출하는 타입이다.

type B = Extract<string | boolean, boolean>;
// boolean

ReturnType< T >

ReturnType은 타입변수 T에 할당된 함수 타입의 반환값 타입을 추출하는 타입이다.

type ReturnType<T extends (...args: any) => any> = T extends (
  ...agrs: any
) => infer R
  ? R
  : never;

function funcA() {
  return "hello";
}

function funcB() {
  return 10;
}

type ReturnA = ReturnType<typeof funcA>;
// string

type ReturnB = ReturnType<typeof funcB>;
// number

0개의 댓글