Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type narrowing context is lost within closures #46118

Closed
ramyhhh opened this issue Sep 28, 2021 · 5 comments Β· Fixed by #56908
Closed

Type narrowing context is lost within closures #46118

ramyhhh opened this issue Sep 28, 2021 · 5 comments Β· Fixed by #56908
Labels
Duplicate An existing issue was already created

Comments

@ramyhhh
Copy link

ramyhhh commented Sep 28, 2021

Bug Report

πŸ”Ž Search Terms

typescript union typed narrowing lost in closures
type narrowing closures

πŸ•— Version & Regression Information

Typescript versions 4.4.3 and still exists on Nightly 4.5.0-dev

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type MyUnion = { type: 'left', propertyExistsOnLeft: string } | { type: 'right', propertyExistsOnRight: string }

let x = {} as MyUnion;

switch (x.type) {
    case 'left':
        const y = x.propertyExistsOnLeft; //all good here ...
        console.log(() => x.propertyExistsOnLeft) //showing error "Property 'propertyExistsOnLeft' does not exist on type 'MyUnion'"
        break;
}

πŸ™ Actual behavior

Type narrowing is working as expected when following control flow, however when the variable is used inside a closures scope (arrow functions or normal functions) it reverts back to it's declared type losing all narrowing inferred from the containing scope.

πŸ™‚ Expected behavior

Type narrowing should continue to work inside the context of closures.

@ramyhhh ramyhhh changed the title Type narrowing lost within closures Type narrowing context is lost within closures Sep 28, 2021
@MartinJohns
Copy link
Contributor

And another duplicate of #9998.

@ramyhhh
Copy link
Author

ramyhhh commented Sep 28, 2021

Yeah, that issue looks like a master issue collecting all control flow analysis issues :)

@MartinJohns
Copy link
Contributor

The exact same issue you describe comes up again and again and again. I probably have seen at least 50 issues already. It's all due to the difficulties described in #9998.

@fatcerberus
Copy link

Yeah, that issue looks like a master issue collecting all control flow analysis issues :)

More specifically, control flow analysis issues across function boundaries. Which is not an easy problem to solve.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Oct 1, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants