Skip to content

Nested unions (and intersections) are eagerly flattened #42075

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

Closed
marijnh opened this issue Dec 22, 2020 · 2 comments
Closed

Nested unions (and intersections) are eagerly flattened #42075

marijnh opened this issue Dec 22, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@marijnh
Copy link

marijnh commented Dec 22, 2020

TypeScript Version: 4.1.3

Search Terms: nested union, flatten union

Code

type T = number | string
let x: T | null // Inspect the type of x

Expected behavior:

Since in most cases, TypeScript takes care to preserve type names, I'd hope x to get type T | null.

Actual behavior:

Union types that are part of another union type are eagerly flattened into the outer type, producing a type printed as number | string | null.

I am running into this when using the TypeScript compiler as a library as part of a documentation generating system. I have some named union types in my API that become noisy and hard to interpret for users when they are expanded every time they appear in some union type.

Playground Link: here

Related Issues: No.

@MartinJohns
Copy link
Contributor

Duplicate of #35654.

@marijnh
Copy link
Author

marijnh commented Dec 23, 2020

I agree, closing this one.

@marijnh marijnh closed this as completed Dec 23, 2020
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 23, 2020
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

No branches or pull requests

3 participants