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
{{ message }}
Repository navigation
functions returning never are not treated the same as raw throw calls in control-flow analysis #9655
functionfail(): never{throw'error';}enumE{A,B}functionf(t: E){lets: string;switch(t){caseE.A:
s='a';break;caseE.B:
s='b';break;default:
fail();// but replacing with "throw 'error"' removes the TS error.}s+='';}
Expected behavior:
s += '' is allowed because all terminating (non-throwing) branches assign to s.
In general, I am working under the assumption that calling functions that returning never are equivalent to raw throw calls during control-flow analysis.
Actual behavior:
ERROR(21,3): : Variable 's' is used before being assigned.
The limitation here has to do with the way the compiler is designed -- control flow analysis happens before typechecking, but we would need type information (as well as identifier resolution) to determine that the fail() call points to a function that is : never, so the s += ''; node is marked as reachable via a path that doesn't assign to s.
The intended fix is to write return fail(); instead of fail();.
Activity
RyanCavanaugh commentedon Jul 12, 2016
The limitation here has to do with the way the compiler is designed -- control flow analysis happens before typechecking, but we would need type information (as well as identifier resolution) to determine that the
fail()call points to a function that is: never, so thes += '';node is marked as reachable via a path that doesn't assign tos.The intended fix is to write
return fail();instead offail();.