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
functionfooWorking(x: number|undefined) : number{if(x===undefined){thrownewError("Is undefined");}returnx;}functionfooBroken(x: number|undefined): number{constisAnError=()=>{thrownewError("Is undefined");}if(x===undefined){isAnError();}returnx;// errors because it can't tell that x can only be a number}
Expected behavior:
fooBroken compiles without issue. Typescript can tell that x must be a number because the if branch calls a function that never returns.
Actual behavior:
Typescript complains on the return statement in fooBroken because it doesn't realize x can only be a number.
TypeScript Version: 3.7.x-dev.201xxxxx
Search Terms:
never type guard
Code
Expected behavior:
fooBroken compiles without issue. Typescript can tell that x must be a number because the if branch calls a function that never returns.
Actual behavior:
Typescript complains on the return statement in fooBroken because it doesn't realize x can only be a number.
Playground Link:
https://www.typescriptlang.org/play/?ssl=17&ssc=2&pln=1&pc=1#code/GYVwdgxgLglg9mABMOcDqcBOBrGYDmAFAB4BciYIAtgEYCmmiAPouACZ3B51sCUi5SrQaIA3gChEiGMEQlEAXiWswHLmB78JUqVAAWmOAHcKdEwFFMhzIQBEASQDOKtdza3eAbkmIAvj8w6KBBMJGJvf3FxUEhYBGRUACFDbDowEkFqekYWdk43XkzhRm1ECARHKGlHAEEwS2tFOX4FAD4xH10DY1MLKyw7Jxd8jXcvH38pGTliRWU89U0OnWq6hoHxqUnEQODQxHDEAHojxAZrZ3oIAEMQRzppKpuwAHIqqDoAG0-EfWuq2bPRAIT4AT0Q9EQ1woWQY4l8QA
Related Issues:
The text was updated successfully, but these errors were encountered: