-
[TS] 타입스크립트 Utility types - Exclude, OmitFrontEnd/TypeScript 2023. 7. 17. 11:57
Exclude, Omit - TS에서 제공하는 type 변형을 유연하게 도와주는 타입들
Exclude<T, U>
Exclude from T those types that are assignable to U. (typescript 2.8 릴리즈)
Exclude<Type, ExcludedUnion> => union type에서 제외된 타입들에 대한 새로운 타입을 정의함
type MyUnion = string | number | boolean; type ExcludeNumber = Exclude<MyUnion, number>; const value: ExcludeNumber = 'hello'; // value can only be of type string or boolean
Omit<Type, Keys>
TypeScript 3.5 introduces the new Omit helper type, which creates a new type with some properties dropped from the original. (typescript 3.5 릴리즈)
Omit 은 어떤 정의된 객체 형태의 타입에서 특정한 프로퍼티들을 제외시켜 줌
type Person = { name: string; age: number; address: string; }; type PersonWithoutAddress = Omit<Person, 'address'>; const person: PersonWithoutAddress = { name: 'John Doe', age: 25, };
만약 원래 정의된 타입에서 특정 key에 대한 타입을 재정의 한 뒤 사용하고 싶다면
import { User } from "@prisma/client"; export type SafeUser = Omit< User, "createdAt" | "updatedAt" | "emailVerified" > & { emailVerified: string | null; //기존에는 DateTime 이었음 createdAt: string; updatedAt: string; };
요약하자면, Exclude는 union type들로부터 제외된 타입들을 사용하고
Omit은 existing type들로부터 생략된 속성들을 사용함
'FrontEnd > TypeScript' 카테고리의 다른 글
zod에 Data 형식 맞춰보기(feat. time) (0) 2024.09.05 [TS] Cannot find module '' or its corresponding type declarations.ts(2307) (0) 2023.02.26 [TypeScript] 제네릭(Generics) 정리 (0) 2022.12.09 [TypeScript] TS에서의 함수 ( 파라미터, 리턴 타입 지정 ) (0) 2022.12.09 [TypeScript] declare module 에 대해 (feat. import, export ) (0) 2022.12.09