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
typeX=string|number;functionisString1(x: X): boolean{if(typeofx==='string'){returntrue;}elseif(typeofx==='number'){returnfalse;}else{x;// Is of never typereturnfalse;// However, need a return to compile}}functionisString2(x: X): boolean{if(typeofx==='string'){returntrue;}elseif(typeofx==='number'){returnfalse;}else{// x is of never type here((a:never)=>{})(x);// Validatereturnfalse;// However, need a return to compile }}functionisString3(x: X): boolean{if(typeofx==='string'){returntrue;}elseif(typeofx==='number'){returnfalse;}else{// x is of never type here((a:never)=>{thrownewError();})(x);// Validate// Does not need a return to compile, is good}}constcompileTimeAssertNever=(x:never)=>{thrownewError('unreachable');};functionisString4(x: X): boolean{if(typeofx==='string'){returntrue;}elseif(typeofx==='number'){returnfalse;}else{// x is of never type herecompileTimeAssertNever(x);// Validatereturnfalse;// However, need a return to compile// Shouldn't this be like isString3?// It would be nice if it was}}
It would be even better if an error doesn't need to be thrown, since once "x" is known to be type "never", the code block can never be reached.
The text was updated successfully, but these errors were encountered:
dingfengquek
changed the title
Need to return value in a "never"
Need to return value in a "never" path
Dec 24, 2016
TypeScript Version: 2.1.4
Code
It would be even better if an error doesn't need to be thrown, since once "x" is known to be type "never", the code block can never be reached.
The text was updated successfully, but these errors were encountered: