Javascript/Typescript
타입스크립트에서 특정 prop을 제외시키기 ( Omit )
bugtype
2019. 12. 1. 14:33
interface Test {
a: string;
b: number;
c: boolean;
}
type OmitA = Omit<Test, "a">; // Equivalent to: {b: number, c: boolean}
type OmitAB = Omit<Test, "a" | "b">; // Equivalent to: {c: boolean}
const a: OmitA = {
a: 'aa',
b: 1,
c: true
}
const b: OmitAB = {
a: 'aa',
b: 1,
c: true
}
Omit 은 optional으로 된 prop이 사라지는 버그가 있었는데 3.5에서 패치가 되었다.
interface Test {
a?: string;
b?: number;
c?: boolean;
}
type OmitA = Omit<Test, "a">; // Equivalent to: {b: number, c: boolean}
const a: OmitA = {
b: 1, // optional이 풀려서 required로 변경된다.
c: true, // optional이 풀려서 required로 변경된다.
}
https://github.com/DefinitelyTyped/DefinitelyTyped/issues/36189