Issue
Why TSC does not give error when function returns null
or undefined
while 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;
}
Solution
You get no error when Type Checking compiler option strictNullChecks
is off
. Enabling the option will result in Typescript displaying the error for the functions add2
and add3
.
You can read more about it: strictnullchecks-off and strictnullchecks-on
Answered By - Siddhant
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.