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

Control flow narrowing sometimes de-aliases unions #42304

Closed
sandersn opened this issue Jan 12, 2021 · 2 comments · Fixed by #42378
Closed

Control flow narrowing sometimes de-aliases unions #42304

sandersn opened this issue Jan 12, 2021 · 2 comments · Fixed by #42378
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@sandersn
Copy link
Member

After #42149, which usually preserves the type aliases of unions as written, some type aliases end up de-aliased in control flow:
(example based on @types/amap-js-api)

type Direction = 'up' | 'down' | 'left' | 'right'
interface Input {
    direction?: Direction
}
declare let input: Input
if (input.direction) {
    input.direction/*should be Direction*/
}

Expected: The inner input.direction: Direction

Actual: The inner input.direction: 'up' | 'down' | 'left' | 'right'

This occurs because control flow narrowing uses filterType, which doesn't understand type aliases or attempt to preserve them. Previously the union-interning behaviour preserved the alias, but that's no longer the case.

@sandersn
Copy link
Member Author

This is a regression in 4.2, although after discussing with @ahejlsberg, we agree that it's one worth taking if there's no obvious way to fix it.

@sandersn sandersn added the Bug A bug in TypeScript label Jan 12, 2021
@sandersn sandersn added this to the TypeScript 4.2.0 milestone Jan 12, 2021
@sandersn
Copy link
Member Author

@ahejlsberg, I'm assigning you for the 4.2 milestone so that @RyanCavanaugh and @DanielRosenwasser are aware of the regression, but feel free to close this when you've had a chance to decide whether it's worth fixing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants