TypeScript

| 와 &의 차이

milliwonkim 2023. 2. 25. 09:53
반응형
SMALL

| => 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')
}

/*
정상 작동
*/

 

반응형
LIST