본문 바로가기
IT/typescript

TS - Extract<T, U>

by 내일은교양왕 2023. 6. 27.

개념
한마디로, T에서 U타입 중 할당 가능한 것만 리턴
정리하지면, pick은 prop을, extract는 type을...

type Extract<T, U> = T extends U ? T : never

 

Examples

type Company = {
  id: string
  name: string
}

type Product = {
  price: number
  rating: number
}

type fakeProduct = Extract<Company | Product, Company>
// type fakeProduct = {
//    id: string;
//    name: string;
// }

 

참고
https://velog.io/@ggong/Typescript의-유틸리티-타입-1-Record-Extract-Pick

'IT > typescript' 카테고리의 다른 글

[Typescript] enum vs union  (0) 2024.02.05
TS - 5.0 New features  (0) 2023.07.01
TS - unknown vs any  (0) 2023.06.27
TS - never  (0) 2023.06.27
TS - Exclude<T, U>  (0) 2023.06.27