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
Currently, even if a type is narrowed down to nothing, we still consider control flow errors in those branches even though you might just be handling error cases:
// Error: Not all code paths return a value.functionfoo(x: string|number){if(typeofx==="string"){returntrue;}elseif(typeofx==="number"){returnfalse;}fail("Unexhaustive!");}functionfail(message: string){throwmessage;}
Similarly, if you add a type annotation, you'll get an error.
// Error: Function lacks ending return statement but return type lacks 'undefined'functionfoo(x: string|number): boolean{if(typeofx==="string"){returntrue;}elseif(typeofx==="number"){returnfalse;}fail("Unexhaustive!");}functionfail(message: string){throwmessage;}
The text was updated successfully, but these errors were encountered:
The issue here is that 1. the compiler does not know that these conditions are exhaustive, sometimes it involves multiple variables, and 2. we do not know that fail always throws.
we could add a syntactic form of defining that a function always throws, but do not think relaxing the check is the solution. there are valid cases, that you want the compiler to catch.
From a discussion with @wycats.
Currently, even if a type is narrowed down to
nothing
, we still consider control flow errors in those branches even though you might just be handling error cases:Similarly, if you add a type annotation, you'll get an error.
The text was updated successfully, but these errors were encountered: