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
typescript
typeguard
type guard
unreachable
undefined
defeat
any permutation thereof
Code
// Please enable TSC option strictNullChecksfunctiononereturn(y: string|undefined){if(!y){thrownewError();}// No error: great.returny.length;}functiontworeturn(y: string|undefined){return3;// temporarily disable functionif(!y){thrownewError();}// Why does this now error?returny.length;}
Expected behavior:
Either:
Consistent type checking regardless of unreachability, or
a "code unreachable" error.
I don't think the unreachable version should necessarily be error free. I just think the current way of erroring is very unintuitive and confusing.
Actual behavior:
The type guard only works if the code is reachable. Otherwise, it stops working, and the error only shows up when you use the variable further downstream as if the type guard had worked.
TypeScript Version:
ts@next (Version 3.4.0-dev.20190313)
Search Terms:
typescript
typeguard
type guard
unreachable
undefined
defeat
any permutation thereof
Code
Expected behavior:
Either:
I don't think the unreachable version should necessarily be error free. I just think the current way of erroring is very unintuitive and confusing.
Actual behavior:
The type guard only works if the code is reachable. Otherwise, it stops working, and the error only shows up when you use the variable further downstream as if the type guard had worked.
Playground Link:
https://www.typescriptlang.org/play/#src=%2F%2F%20Please%20enable%20TSC%20option%20strictNullChecks%0D%0A%0D%0Afunction%20onereturn(y%3A%20string%20%7C%20undefined)%20%7B%0D%0A%20%20%20%20if%20(!y)%20%7B%0D%0A%20%20%20%20%20%20%20%20throw%20new%20Error()%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20%2F%2F%20No%20error%3A%20great.%0D%0A%20%20%20%20return%20y.length%3B%0D%0A%7D%0D%0A%0D%0Afunction%20tworeturn(y%3A%20string%20%7C%20undefined)%20%7B%0D%0A%20%20%20%20return%203%3B%0D%0A%20%20%20%20if%20(!y)%20%7B%0D%0A%20%20%20%20%20%20%20%20throw%20new%20Error()%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20%2F%2F%20Why%20does%20this%20still%20error%3F%0D%0A%20%20%20%20return%20y.length%3B%0D%0A%7D
Related Issues:
Couldn't find anything related in bug tracker.
The text was updated successfully, but these errors were encountered: