Skip to content

Intersection of non-null and null is not deduced to be never #33274

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
Muon opened this issue Sep 6, 2019 · 2 comments
Closed

Intersection of non-null and null is not deduced to be never #33274

Muon opened this issue Sep 6, 2019 · 2 comments
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@Muon
Copy link

Muon commented Sep 6, 2019

The intersection of non-null/undefined types with null and undefined is not deduced to be never. Instead, things like string & null persist unchanged. (This is with strict null checking.) This also applies to singleton types, so the type "a" & "b" is not deduced to be never.

TypeScript Version: 3.5.1

Search Terms: primitive null intersection never

Code

type T = string & null;
type U = T extends never ? number : never;
const a: U = 1;

Expected behavior: T is deduced to be never, so U is number, so it compiles.

Actual behavior: The code does not compile because T is not deduced to be never and so U is never.

Playground Link: https://www.typescriptlang.org/play/#code/C4TwDgpgBAKlC8UDOwBOBLAdgcygMikwFcAbEgbgChRIoBVBWKCAD2AkwBMlCIA3CKigB+QkQC2AI0FQAXLwGoqAYwD2mFFACG8hogCM5IA

@jack-williams
Copy link
Collaborator

Fixed by #31838.

@Muon
Copy link
Author

Muon commented Sep 6, 2019

Ah, thank you. Reading through that PR, it seems that pre-3.6, the reduction can be forced by taking a union, so e.g. (string & null) | never reduces to never.

@sandersn sandersn added Bug A bug in TypeScript Fix Available A PR has been opened for this issue labels Sep 6, 2019
@sandersn sandersn closed this as completed Sep 6, 2019
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

No branches or pull requests

3 participants