pebblepark
개발계발
pebblepark
전체 방문자
오늘
어제
  • 분류 전체보기 (24)
    • Frontend (7)
    • Backend (7)
    • 인프라 (1)
    • CS (0)
      • Design Pattern (0)
    • 정리용 (9)
    • 회고 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • useLayoutEffect
  • springboot
  • redux
  • 스프링
  • 리액트쿼리
  • vite
  • spring
  • javascript
  • wsl
  • 스프링 의존관계
  • 스프링 컨테이너
  • hoisting
  • typescript
  • react
  • react-query
  • 무한스크롤
  • TDZ
  • React Query
  • Context API
  • debounce
  • ERR_UNSAFE_PORT
  • SpringMVC
  • Github Pages
  • github
  • @ModelAttribute
  • Git
  • CORS
  • Docker
  • 호이스팅
  • 스프링 빈

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
pebblepark

개발계발

[Typescript] Advanced-Types & Utility-Types 정리
Frontend

[Typescript] Advanced-Types & Utility-Types 정리

2022. 3. 2. 22:26

infer

  • 타입변수를 참조하기 위해 사용
  • 조건부 타입의 extends 절에서 infer 키워드 사용 가능

 

example

type First<T> = T extends [infer U, ...unknown[]] ? U : never;

type SomeTupleType = [string, number, boolean];
type FirstElementType = First<SomeTupleType>; // string

 

example: infer 키워드 여러개 사용

type Unpacked<T> =
    T extends (infer U)[] ? U :
    T extends (...args: any[]) => infer U ? U :
    T extends Promise<infer U> ? U :
    T;

type T0 = Unpacked<string>;  // string
type T1 = Unpacked<string[]>;  // string
type T2 = Unpacked<() => string>;  // string
type T3 = Unpacked<Promise<string>>;  // string
type T4 = Unpacked<Promise<string>[]>;  // Promise<string>
type T5 = Unpacked<Unpacked<Promise<string>[]>>;  // string

// 위의 예제를 응용하면
type UnpackedAxiosResponse<T> = 
    T extends (...args: any) => Promise<AxiosResponse<infer U>> ? U : T;

// Pet
type Response = 
    UnpackedAxiosResponse<(petId: number) => Promise<AxiosResponse<Pet, any>>>;



Parameters<T>

  • 함수 타입 T의 매개변수 타입들의 튜플 타입 구성
/**
* Obtain the parameters of a function type in a tuple
*/
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;

 

example

declare function f1(arg: { a: number, b: string }): void;

type T0 = Parameters<() => string>;  // []
type T1 = Parameters<(s: string) => void>;  // [string]
type T2 = Parameters<(<T>(arg: T) => T)>;  // [unknown]
type T3 = Parameters<typeof f1>;  // [{ a: number, b: string }]
type T4 = Parameters<any>;  // unknown[]
type T5 = Parameters<never>;  // never
type T6 = Parameters<string>;  // 오류
type T7 = Parameters<Function>;  // 오류



ReturnType<T>

  • 함수 T의 반환 타입으로 구성된 타입
/**
* Obtain the return type of a function type
*/
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;

 

example

declare function f1(): { a: number, b: string }
type T0 = ReturnType<() => string>;  // string
type T1 = ReturnType<(s: string) => void>;  // void
type T2 = ReturnType<<T>() => T>;  // {}
type T3 = ReturnType<<T extends U, U extends number[]>() => T>; // number[]
type T4 = ReturnType<typeof f1>;  // { a: number, b: string }
type T5 = ReturnType<any>;  // any
type T6 = ReturnType<never>;  // any
type T7 = ReturnType<string>;  // 오류
type T8 = ReturnType<Function>;  // 오류



typeof

*참고: javascript에는 이미 typeof 연산자가 정의되어 있음
console.log(typeof "Hello, world!"); // "string"​

 

  • 변수 또는 속성의 유형을 참조하기 위해 사용 가능함
let s = "hello";
let n: typeof s; // let n: string;



참고

Typescript-유틸리티 타입
[TypeScript] Advanced Types(고급 타입)
Typeof Type Operator

 

 

 

저작자표시 (새창열림)

'Frontend' 카테고리의 다른 글

React-Query 정리  (0) 2022.08.05
[Javascript] Throttle 과 Debounce  (0) 2022.05.12
[Redux] 간단한 예제로 살펴보는 리덕스의 동작 원리  (0) 2022.03.17
[React] ContextAPI & useContext Hook을 통한 Global State 값 관리하기  (0) 2022.03.11
[Javascript] 변수, 호이스팅, TDZ  (0) 2022.03.07
    'Frontend' 카테고리의 다른 글
    • [Javascript] Throttle 과 Debounce
    • [Redux] 간단한 예제로 살펴보는 리덕스의 동작 원리
    • [React] ContextAPI & useContext Hook을 통한 Global State 값 관리하기
    • [Javascript] 변수, 호이스팅, TDZ
    pebblepark
    pebblepark
    프론트엔드 개발자입니다. 피드백은 언제나 환영입니다:)

    티스토리툴바