이전시간엔 타입선언과 타입을 적용하는방법에 대해서 알아보았다.
이번시간에는 이미 선안된 타입들을 관리할수있는 유틸타입에대해서 알아보자.
T의 모든 프로퍼티를 선택적(optional)으로 만드는 타입을 구성한다
interface Todo{
title:string
description:string
}
type makePartial =Partial<Todo>
const getPartial:makePartial={
// 모두 선택적 프로퍼티가 된다.
}
T의 모든 프로퍼티를 readOnly상태로 만든다
interface NotReadOnly {
title: string;
}
const readOnlyTypeVariable: Readonly<NotReadOnly> = {
title: "이제난 리드온니이다.",
};
readOnlyTypeVariable.title="ㅇㅋ" // 에러
오브젝트의 키는 K타입 밸류는 T타입인 타입을 만든다.
type keyType="home"|"about"|"contact"
interface ValueInterface{
title:string
}
const record:Record<keyType,ValueInterface>={
home:{title:"집"}
about:{title:"정보"}
contact:{title:"접근"}
}
T타입에서 K의 키값만 선택하여 새로운 타입을 반환한다
interface TypeList {
title: string;
description: string;
completed: boolean;
}
type iWillPickType = Pick<TypeList, "completed" | "description">;
const newType: iWillPickType = {
description: "asd",
completed: false,
// title 이 타입은 선택안했으니 에러발생
};
T타입에서 K의 키값만 제거한뒤 새로운 타입을 반환
interface TypeList {
title: string;
description: string;
completed: boolean;
}
type iWillRemoveType = Omit<TypeList, "completed">;
const newOmitType: iWillRemoveType = {
description: "asdasd",
title: "asdasd",
//completed 이부분은 제거되었기때문에 에러발생
};
T의 타입들중 ,의 타입을 제거한뒤 할당한다.
interface TypeList1 {
typeList1title: string;
typeList1description: string;
typeList1completed: boolean;
}
interface TypeList2 {
typeList2Title: string;
typeList2description: string;
typeList2completed: boolean;
}
type test = Exclude<TypeList1 | TypeList2, TypeList1>;
const ssss: test = {
// T에서 TypeList1가 제거되어 TypeList2 타입만 할당된다.
};
T의 타입들중에서 U의 타입만 추출
interface TypeList1 {
typeList1title: string;
typeList1description: string;
typeList1completed: boolean;
}
interface TypeList2 {
typeList2Title: string;
typeList2description: string;
typeList2completed: boolean;
}
type test = Extract<TypeList1 | TypeList2, TypeList1>;
const ssss: test = {
// T에서 U만 추가하여 TypeList1의 타입만 들어간다.
};
T타입들중에 nullabel한 타입을 제거한다.(falsy한 타입은 유지한다)
type T0 = NonNullable<string | number | undefined|false>; // string|number|false
type T1 = NonNullable<string[] | null | undefined>; // string[]