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
functionfind(item: string): void{// some logic here}functionexample(items: Map<string,string>): string|void{consta=items.get("1");returna;// The following check ensures `a` is not undefined.if(!a)return"No A";// Even after the above check, TypeScript throws an error for the line below,// indicating that `a` can be undefined.constb=find(a);// Error: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.// Type 'undefined' is not assignable to type 'string'.}
π Actual behavior
When there's unreachable code following a return statement, TypeScript's control flow analysis seems not to correctly narrow the type based on the checks present in the unreachable code.
TypeScript throws an error indicating a can be undefined.
π Expected behavior
Even though the code after the return statement is unreachable, TypeScript should recognize that the check if (!a) ensures a cannot be undefined when passed to find(a).
Additional information and Questions
Firstly, could you please confirm if this behavior is indeed unintended and not by design?
If this is an issue worth fixing, could you point me towards the relevant parts of the codebase or any related issues/pull requests that might give me a head start?
I am eager to take this up and provide a fix. I hope this can be a valuable contribution to the TypeScript community.
The text was updated successfully, but these errors were encountered:
Hey @RyanCavanaugh, I see the issue is still open. if it needs to be fixed, I'm very keen on contributing and helping resolve it.
Would it be possible to get some pointers or guidance on where to start or any context from the ongoing discussions on the original issue?
Uh oh!
There was an error while loading. Please reload this page.
π Search Terms
"code after return", "dead code typecheck"
π Version & Regression Information
β― Playground Link
Typescript Lang Link
π» Code
π Actual behavior
When there's unreachable code following a return statement, TypeScript's control flow analysis seems not to correctly narrow the type based on the checks present in the unreachable code.
TypeScript throws an error indicating
a
can be undefined.π Expected behavior
Even though the code after the return statement is unreachable, TypeScript should recognize that the check
if (!a)
ensuresa
cannot beundefined
when passed tofind(a)
.Additional information and Questions
The text was updated successfully, but these errors were encountered: