Why does this code throw a Typescript error?
const foo: number | undefined = 22
function bar(x: Date | undefined) {
!!x && console.log(x) || console.log('x is undefined')
}
bar(foo && new Date())
^^^^^^^^^^^^^^^^^
TS2345: Argument of type '0 | Date' is not assignable to parameter of type 'Date | undefined'. Type 'number' is not assignable to type 'Date'
Seems like there are two cases:
foois anumber→foo && new Date()is of typeDatefooisundefined→foo && new Date()is of typeundefined
Where does the 0 type come from here?