[TS] Type | 04. Pick & Omit

2024. 7. 11. 22:29·Language/TypeScript

Pick

Pick은 기존에 존재하던 타입에서 특정 요소만 선택해 구성되는 새로운 타입입니다.

type typePick = Pick<targetType, typeExtracting>;

생성된 타입은 다음과 같이 선택한 요소들만 지니게 됩니다.

interface Product {
  id: string;
  name: string;
  price: number;
  membersOnly?: boolean;
}

type ProductInfo = Pick<Product, 'name' | 'price'>;

const product: ProductInfo = {
    name: "shirt";
    price: "120";
}

Omit

Omit은 기존에 존재하던 타입에서 특정 요소만 제외해 구성되는 새로운 타입입니다.

type typeOmit = Omit<targetType, typeIgnoring>;

생성된 타입은 다음과 같이 선택하지 않은 요소들만 지니게 됩니다.

interface Product {
  id: string;
  name: string;
  price: number;
  membersOnly?: boolean;
}

type ProductInfo = Omit<Product, 'id' | 'membersOnly'>;

const product: ProductInfo = {
    name: "shirt";
    price: "120";
}
728x90

'Language > TypeScript' 카테고리의 다른 글

[TS] Type | 05. Parameters & ReturnType  (0) 2024.07.12
[TS] outDir & rootDir  (0) 2024.07.11
[TS] Type | 03. Record  (0) 2024.07.10
[TS] Syntax | 07. keyof & typeof  (0) 2024.07.10
[TS] Syntax | 06. Union & Intersection  (0) 2024.06.28
'Language/TypeScript' 카테고리의 다른 글
  • [TS] Type | 05. Parameters & ReturnType
  • [TS] outDir & rootDir
  • [TS] Type | 03. Record
  • [TS] Syntax | 07. keyof & typeof
Rayi
Rayi
  • Rayi
    아카이브
    Rayi
  • 전체
    오늘
    어제
    • 분류 전체보기 (276)
      • CS (40)
        • CV (2)
        • PS (34)
      • Reveiw (18)
        • Paper (18)
        • Github (0)
      • ML (8)
        • Pytorch (5)
      • Language (59)
        • Python (8)
        • JavaScript (32)
        • TypeScript (16)
        • C++ (3)
      • IDE (12)
      • Git (13)
      • Frontend (77)
        • React (8)
        • ReactNative (6)
        • SolidJS (20)
        • CSS (12)
      • Backend (44)
        • DB (18)
        • Node.js (11)
      • UI (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    python
    DB
    ML
    react
    ps
    modal
    backend
    Three
    API
    PRISMA
    deploy
    CSS
    review
    CS
    js
    ts
    expo
    frontend
    vscode
    PyTorch
    mongo
    CV
    postgresql
    figma
    SOLID
    GAN
    nodejs
    ReactNative
    Express
    Git
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
Rayi
[TS] Type | 04. Pick & Omit
상단으로

티스토리툴바