Partial<Type>
Partial은 Type의 모든 속성들을 optional로 만들어 준다.
interface bookDetail{
title: string;
author: string;
page: number
}
type PBook = Partial<bookDetail>;
const harryPoter : PBook = {
title: "harryPoter",
author: "J.K",
}
/*
PBook {
title?: string | undefined;
author?: string | undefined;
page?: number | undefined;
}
*/
Required<Type>
Required는 Type에서 optional을 제거해 준다.
interface books{
name? : string;
author? : string;
}
type RBooks = Required<books>;
const harryPoter: RBooks = {
name : 'arryPoter',
author : 'J. K'
}
/*
RBooks = {
name : string;
author : string;
}
*/
Pick<Type, Keys>
Pick은 Type에서 keys의 타입들만 정의된 타입을 생성한다.
interface bookDetail{
title: string;
author: string;
page: number
}
type PickB = Pick<bookDetail, "title" | "page">
const harryPoter : PickB = {
title: "harryPoter",
page: 200
}
/*
type PickB = {
title: string;
page: number;
}
*/
Omit<Type, Keys>
Omit은 Type에서 Keys를 제거한 타입을 생성한다.
interface bookDetail{
title: string;
author: string;
page: number
}
type OmitB = Omit<bookDetail, "author">
const harryPoter : OmitB = {
title: "harryPoter",
page: 200
}
/*
type OmitB = {
title: string;
page: number;
}
*/
Exclude<Type, ExcludenUnion>
Exclude는 Type에서 ExcludenUnion의 union 되어있는 타입들을 모두 제거하여 타입을 생성한다.
type A = string | number | boolean;
type ExcludeA = Exclude<A , boolean>;
const B: ExcludeA = 'hello';
const C: ExcludeA = 10;
/*
type ExcludeA = string | number
*/
Extract<type, Union>
Extract는 Type에서 Union의 union된 타입들만을 가져와 타입을 생성한다.
type A = string | number | boolean;
type ExtractA = Extract<A , string | number>;
const B: ExtractA = 'hello';
const C: ExtractA = 10;
/*
type ExtractA = string | number
*/
https://www.typescriptlang.org/ko/docs/handbook/utility-types.html
'Language > TypeScript' 카테고리의 다른 글
#7 TypeScript Optional, callback의 매개변수 (0) | 2023.09.02 |
---|---|
#6 TypeScript 제네릭(Generics) (0) | 2023.09.02 |
#5 TypeScript 클래스(Class) (0) | 2023.09.01 |
#4 TypeScript 타입 추론(Inference), 단언(Assertion), 가드(Guard) (0) | 2023.08.30 |
#3 TypeScript 타입의 생성 (0) | 2023.08.30 |