Skip to main content

|와 &의 차이

| => javascript의 || 느낌 (or)

& => javascript의 && 느낌 (and)

interface Person {
name: string;
}

interface Liftspan {
birth: Date;
death?: Date;
}

type PersonSpan = Person & Liftspan;

const spans: PersonSpan = {
name: "kiwon",
// birth: new Date('2022-06-09'),
// death: new Date('2122-06-09')
};

/**
Type '{ name: string; death: Date; }' is not assignable to type 'PersonSpan'.
Property 'birth' is missing in type '{ name: string; death: Date; }'
but required in type 'Liftspan'.(2322) input.tsx(6, 5): 'birth' is declared here.
*/
type PersonSpan = Person | Liftspan;

const spans: PersonSpan = {
name: "kiwon",
// birth: new Date('2022-06-09'),
// death: new Date('2122-06-09')
};

/*
정상 작동
*/