You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms: type guard function optional overload
Code
classMyClass{foo(){}}functionadd(test: number): void;functionadd(test: object,paramIfObject: MyClass): void;// should overload this declarationfunctionadd(test: number|object,paramIfObject?: MyClass): void{if(typeoftest==='object'){// Indicated the argument to overload is not optionalparamIfObject.foo();// Error: Object is possibly 'undefined'.(2532)}}
Expected behavior: Compile successfully
Actual behavior: Error: Object is possibly 'undefined'.(2532)
Description: The variable paramIfObject was used inside the if statement, which should be a type guard refering to the correct function overload. So the variable should not compiled as an optional argument. Just equals to this: (no error)
TypeScript Version: 3.8.3
Search Terms: type guard function optional overload
Code
Expected behavior: Compile successfully
Actual behavior: Error: Object is possibly 'undefined'.(2532)
Description: The variable
paramIfObject
was used inside the if statement, which should be a type guard refering to the correct function overload. So the variable should not compiled as an optional argument. Just equals to this: (no error)Playground Link: Playground Link
Related Issues:
Compiler Options
The text was updated successfully, but these errors were encountered: