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
StrictNullChecks and throw/return with incorrect when used with closure scope #13572
Do not emit an error about host possibly being null, since the method exits early if that's the case.
Actual behavior:
Typically when using strictnullchecks Typescript is intelligent enough to understand that when you throw an error from a method based on a type check/guard it narrows the type after the statement. As indicated in this short example:
functiontest(hosts: {whatever: boolean}[]){lethost=hosts.filter((f)=>f.whatever).shift();// Correctly calls out hose as possibly being nullconsole.log(`${host.whatever} selected`);if(!host){thrownewError("Crap!");}// Does not complain here because of the above check/throwconsole.log(`${host.whatever} selected`);}
For some reason when you add the return of the function and use host through a closure it starts complaining.
I'm sure it's the closure, but I don't see how this could error.
Activity
tlong-dev commentedon Jan 19, 2017
Note: re-assigning the variable to a new variable after the throw seems to re-bind the type information and solves the issue... Seems like a bug.
HerringtonDarkholme commentedon Jan 19, 2017
#9998 / #11498
Changing
let hosttoconst hostalso solves this.tlong-dev commentedon Jan 19, 2017
Wow, the perfect comment!
Found the threads I knew existed by couldn't locate in search, and offered clean workaround!
Thanks. Closing in favor of #9998 and #11498