Skip to content

Control flow narrowing sometimes de-aliases unions #42304

Closed

Description

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions