Why does TypeScript not show an error when a function returns null
or undefined
while the function's return type is number
.
//gives error
//Error : A function whose declared type is neither 'void' nor 'any' must return a value.ts(2355)
function add1(a: number, b: number): number {}
// no error
function add2(a: number, b: number): number {
return undefined;
}
// no error
function add3(a: number, b: number): number {
return null;
}