-
Notifications
You must be signed in to change notification settings - Fork 12.8k
{} & null and {} & undefined should always be never #50553
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//// [NonNullableInNonStrictMode.ts] | ||
// These should all resolve to never | ||
|
||
type T0 = NonNullable<null>; | ||
type T1 = NonNullable<undefined>; | ||
type T2 = null & {}; | ||
type T3 = undefined & {}; | ||
type T4 = null & undefined; | ||
type T6 = null & { a: string } & {}; | ||
|
||
// Repro from #50519 | ||
|
||
type NonNullableNew<T> = T & {}; | ||
type NonNullableOld<T> = T extends null | undefined ? never : T; | ||
|
||
type IsNullWithoutStrictNullChecks = NonNullableNew<null>; | ||
type IsAlwaysNever = NonNullableOld<null>; | ||
|
||
|
||
//// [NonNullableInNonStrictMode.js] | ||
// These should all resolve to never |
45 changes: 45 additions & 0 deletions
45
tests/baselines/reference/NonNullableInNonStrictMode.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
=== tests/cases/compiler/NonNullableInNonStrictMode.ts === | ||
// These should all resolve to never | ||
|
||
type T0 = NonNullable<null>; | ||
>T0 : Symbol(T0, Decl(NonNullableInNonStrictMode.ts, 0, 0)) | ||
>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T1 = NonNullable<undefined>; | ||
>T1 : Symbol(T1, Decl(NonNullableInNonStrictMode.ts, 2, 28)) | ||
>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T2 = null & {}; | ||
>T2 : Symbol(T2, Decl(NonNullableInNonStrictMode.ts, 3, 33)) | ||
|
||
type T3 = undefined & {}; | ||
>T3 : Symbol(T3, Decl(NonNullableInNonStrictMode.ts, 4, 20)) | ||
|
||
type T4 = null & undefined; | ||
>T4 : Symbol(T4, Decl(NonNullableInNonStrictMode.ts, 5, 25)) | ||
|
||
type T6 = null & { a: string } & {}; | ||
>T6 : Symbol(T6, Decl(NonNullableInNonStrictMode.ts, 6, 27)) | ||
>a : Symbol(a, Decl(NonNullableInNonStrictMode.ts, 7, 18)) | ||
|
||
// Repro from #50519 | ||
|
||
type NonNullableNew<T> = T & {}; | ||
>NonNullableNew : Symbol(NonNullableNew, Decl(NonNullableInNonStrictMode.ts, 7, 36)) | ||
>T : Symbol(T, Decl(NonNullableInNonStrictMode.ts, 11, 20)) | ||
>T : Symbol(T, Decl(NonNullableInNonStrictMode.ts, 11, 20)) | ||
|
||
type NonNullableOld<T> = T extends null | undefined ? never : T; | ||
>NonNullableOld : Symbol(NonNullableOld, Decl(NonNullableInNonStrictMode.ts, 11, 32)) | ||
>T : Symbol(T, Decl(NonNullableInNonStrictMode.ts, 12, 20)) | ||
>T : Symbol(T, Decl(NonNullableInNonStrictMode.ts, 12, 20)) | ||
>T : Symbol(T, Decl(NonNullableInNonStrictMode.ts, 12, 20)) | ||
|
||
type IsNullWithoutStrictNullChecks = NonNullableNew<null>; | ||
>IsNullWithoutStrictNullChecks : Symbol(IsNullWithoutStrictNullChecks, Decl(NonNullableInNonStrictMode.ts, 12, 64)) | ||
>NonNullableNew : Symbol(NonNullableNew, Decl(NonNullableInNonStrictMode.ts, 7, 36)) | ||
|
||
type IsAlwaysNever = NonNullableOld<null>; | ||
>IsAlwaysNever : Symbol(IsAlwaysNever, Decl(NonNullableInNonStrictMode.ts, 14, 58)) | ||
>NonNullableOld : Symbol(NonNullableOld, Decl(NonNullableInNonStrictMode.ts, 11, 32)) | ||
|
43 changes: 43 additions & 0 deletions
43
tests/baselines/reference/NonNullableInNonStrictMode.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
=== tests/cases/compiler/NonNullableInNonStrictMode.ts === | ||
// These should all resolve to never | ||
|
||
type T0 = NonNullable<null>; | ||
>T0 : never | ||
>null : null | ||
|
||
type T1 = NonNullable<undefined>; | ||
>T1 : never | ||
|
||
type T2 = null & {}; | ||
>T2 : never | ||
>null : null | ||
|
||
type T3 = undefined & {}; | ||
>T3 : never | ||
|
||
type T4 = null & undefined; | ||
>T4 : never | ||
>null : null | ||
|
||
type T6 = null & { a: string } & {}; | ||
>T6 : never | ||
>null : null | ||
>a : string | ||
|
||
// Repro from #50519 | ||
|
||
type NonNullableNew<T> = T & {}; | ||
>NonNullableNew : NonNullableNew<T> | ||
|
||
type NonNullableOld<T> = T extends null | undefined ? never : T; | ||
>NonNullableOld : NonNullableOld<T> | ||
>null : null | ||
|
||
type IsNullWithoutStrictNullChecks = NonNullableNew<null>; | ||
>IsNullWithoutStrictNullChecks : never | ||
>null : null | ||
|
||
type IsAlwaysNever = NonNullableOld<null>; | ||
>IsAlwaysNever : never | ||
>null : null | ||
|
21 changes: 21 additions & 0 deletions
21
tests/baselines/reference/nonNullableAndObjectIntersections(strict=false).js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//// [nonNullableAndObjectIntersections.ts] | ||
// These should all resolve to never | ||
|
||
type T0 = NonNullable<null>; | ||
type T1 = NonNullable<undefined>; | ||
type T2 = null & {}; | ||
type T3 = undefined & {}; | ||
type T4 = null & undefined; | ||
type T6 = null & { a: string } & {}; | ||
|
||
// Repro from #50519 | ||
|
||
type NonNullableNew<T> = T & {}; | ||
type NonNullableOld<T> = T extends null | undefined ? never : T; | ||
|
||
type TestNew = NonNullableNew<null>; | ||
type TestOld = NonNullableOld<null>; | ||
|
||
|
||
//// [nonNullableAndObjectIntersections.js] | ||
// These should all resolve to never |
45 changes: 45 additions & 0 deletions
45
tests/baselines/reference/nonNullableAndObjectIntersections(strict=false).symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
=== tests/cases/compiler/nonNullableAndObjectIntersections.ts === | ||
// These should all resolve to never | ||
|
||
type T0 = NonNullable<null>; | ||
>T0 : Symbol(T0, Decl(nonNullableAndObjectIntersections.ts, 0, 0)) | ||
>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T1 = NonNullable<undefined>; | ||
>T1 : Symbol(T1, Decl(nonNullableAndObjectIntersections.ts, 2, 28)) | ||
>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T2 = null & {}; | ||
>T2 : Symbol(T2, Decl(nonNullableAndObjectIntersections.ts, 3, 33)) | ||
|
||
type T3 = undefined & {}; | ||
>T3 : Symbol(T3, Decl(nonNullableAndObjectIntersections.ts, 4, 20)) | ||
|
||
type T4 = null & undefined; | ||
>T4 : Symbol(T4, Decl(nonNullableAndObjectIntersections.ts, 5, 25)) | ||
|
||
type T6 = null & { a: string } & {}; | ||
>T6 : Symbol(T6, Decl(nonNullableAndObjectIntersections.ts, 6, 27)) | ||
>a : Symbol(a, Decl(nonNullableAndObjectIntersections.ts, 7, 18)) | ||
|
||
// Repro from #50519 | ||
|
||
type NonNullableNew<T> = T & {}; | ||
>NonNullableNew : Symbol(NonNullableNew, Decl(nonNullableAndObjectIntersections.ts, 7, 36)) | ||
>T : Symbol(T, Decl(nonNullableAndObjectIntersections.ts, 11, 20)) | ||
>T : Symbol(T, Decl(nonNullableAndObjectIntersections.ts, 11, 20)) | ||
|
||
type NonNullableOld<T> = T extends null | undefined ? never : T; | ||
>NonNullableOld : Symbol(NonNullableOld, Decl(nonNullableAndObjectIntersections.ts, 11, 32)) | ||
>T : Symbol(T, Decl(nonNullableAndObjectIntersections.ts, 12, 20)) | ||
>T : Symbol(T, Decl(nonNullableAndObjectIntersections.ts, 12, 20)) | ||
>T : Symbol(T, Decl(nonNullableAndObjectIntersections.ts, 12, 20)) | ||
|
||
type TestNew = NonNullableNew<null>; | ||
>TestNew : Symbol(TestNew, Decl(nonNullableAndObjectIntersections.ts, 12, 64)) | ||
>NonNullableNew : Symbol(NonNullableNew, Decl(nonNullableAndObjectIntersections.ts, 7, 36)) | ||
|
||
type TestOld = NonNullableOld<null>; | ||
>TestOld : Symbol(TestOld, Decl(nonNullableAndObjectIntersections.ts, 14, 36)) | ||
>NonNullableOld : Symbol(NonNullableOld, Decl(nonNullableAndObjectIntersections.ts, 11, 32)) | ||
|
43 changes: 43 additions & 0 deletions
43
tests/baselines/reference/nonNullableAndObjectIntersections(strict=false).types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
=== tests/cases/compiler/nonNullableAndObjectIntersections.ts === | ||
// These should all resolve to never | ||
|
||
type T0 = NonNullable<null>; | ||
>T0 : never | ||
>null : null | ||
|
||
type T1 = NonNullable<undefined>; | ||
>T1 : never | ||
|
||
type T2 = null & {}; | ||
>T2 : never | ||
>null : null | ||
|
||
type T3 = undefined & {}; | ||
>T3 : never | ||
|
||
type T4 = null & undefined; | ||
>T4 : never | ||
>null : null | ||
|
||
type T6 = null & { a: string } & {}; | ||
>T6 : never | ||
>null : null | ||
>a : string | ||
|
||
// Repro from #50519 | ||
|
||
type NonNullableNew<T> = T & {}; | ||
>NonNullableNew : NonNullableNew<T> | ||
|
||
type NonNullableOld<T> = T extends null | undefined ? never : T; | ||
>NonNullableOld : NonNullableOld<T> | ||
>null : null | ||
|
||
type TestNew = NonNullableNew<null>; | ||
>TestNew : never | ||
>null : null | ||
|
||
type TestOld = NonNullableOld<null>; | ||
>TestOld : never | ||
>null : null | ||
|
22 changes: 22 additions & 0 deletions
22
tests/baselines/reference/nonNullableAndObjectIntersections(strict=true).js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//// [nonNullableAndObjectIntersections.ts] | ||
// These should all resolve to never | ||
|
||
type T0 = NonNullable<null>; | ||
type T1 = NonNullable<undefined>; | ||
type T2 = null & {}; | ||
type T3 = undefined & {}; | ||
type T4 = null & undefined; | ||
type T6 = null & { a: string } & {}; | ||
|
||
// Repro from #50519 | ||
|
||
type NonNullableNew<T> = T & {}; | ||
type NonNullableOld<T> = T extends null | undefined ? never : T; | ||
|
||
type TestNew = NonNullableNew<null>; | ||
type TestOld = NonNullableOld<null>; | ||
|
||
|
||
//// [nonNullableAndObjectIntersections.js] | ||
"use strict"; | ||
// These should all resolve to never |
45 changes: 45 additions & 0 deletions
45
tests/baselines/reference/nonNullableAndObjectIntersections(strict=true).symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
=== tests/cases/compiler/nonNullableAndObjectIntersections.ts === | ||
// These should all resolve to never | ||
|
||
type T0 = NonNullable<null>; | ||
>T0 : Symbol(T0, Decl(nonNullableAndObjectIntersections.ts, 0, 0)) | ||
>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T1 = NonNullable<undefined>; | ||
>T1 : Symbol(T1, Decl(nonNullableAndObjectIntersections.ts, 2, 28)) | ||
>NonNullable : Symbol(NonNullable, Decl(lib.es5.d.ts, --, --)) | ||
|
||
type T2 = null & {}; | ||
>T2 : Symbol(T2, Decl(nonNullableAndObjectIntersections.ts, 3, 33)) | ||
|
||
type T3 = undefined & {}; | ||
>T3 : Symbol(T3, Decl(nonNullableAndObjectIntersections.ts, 4, 20)) | ||
|
||
type T4 = null & undefined; | ||
>T4 : Symbol(T4, Decl(nonNullableAndObjectIntersections.ts, 5, 25)) | ||
|
||
type T6 = null & { a: string } & {}; | ||
>T6 : Symbol(T6, Decl(nonNullableAndObjectIntersections.ts, 6, 27)) | ||
>a : Symbol(a, Decl(nonNullableAndObjectIntersections.ts, 7, 18)) | ||
|
||
// Repro from #50519 | ||
|
||
type NonNullableNew<T> = T & {}; | ||
>NonNullableNew : Symbol(NonNullableNew, Decl(nonNullableAndObjectIntersections.ts, 7, 36)) | ||
>T : Symbol(T, Decl(nonNullableAndObjectIntersections.ts, 11, 20)) | ||
>T : Symbol(T, Decl(nonNullableAndObjectIntersections.ts, 11, 20)) | ||
|
||
type NonNullableOld<T> = T extends null | undefined ? never : T; | ||
>NonNullableOld : Symbol(NonNullableOld, Decl(nonNullableAndObjectIntersections.ts, 11, 32)) | ||
>T : Symbol(T, Decl(nonNullableAndObjectIntersections.ts, 12, 20)) | ||
>T : Symbol(T, Decl(nonNullableAndObjectIntersections.ts, 12, 20)) | ||
>T : Symbol(T, Decl(nonNullableAndObjectIntersections.ts, 12, 20)) | ||
|
||
type TestNew = NonNullableNew<null>; | ||
>TestNew : Symbol(TestNew, Decl(nonNullableAndObjectIntersections.ts, 12, 64)) | ||
>NonNullableNew : Symbol(NonNullableNew, Decl(nonNullableAndObjectIntersections.ts, 7, 36)) | ||
|
||
type TestOld = NonNullableOld<null>; | ||
>TestOld : Symbol(TestOld, Decl(nonNullableAndObjectIntersections.ts, 14, 36)) | ||
>NonNullableOld : Symbol(NonNullableOld, Decl(nonNullableAndObjectIntersections.ts, 11, 32)) | ||
|
43 changes: 43 additions & 0 deletions
43
tests/baselines/reference/nonNullableAndObjectIntersections(strict=true).types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
=== tests/cases/compiler/nonNullableAndObjectIntersections.ts === | ||
// These should all resolve to never | ||
|
||
type T0 = NonNullable<null>; | ||
>T0 : never | ||
>null : null | ||
|
||
type T1 = NonNullable<undefined>; | ||
>T1 : never | ||
|
||
type T2 = null & {}; | ||
>T2 : never | ||
>null : null | ||
|
||
type T3 = undefined & {}; | ||
>T3 : never | ||
|
||
type T4 = null & undefined; | ||
>T4 : never | ||
>null : null | ||
|
||
type T6 = null & { a: string } & {}; | ||
>T6 : never | ||
>null : null | ||
>a : string | ||
|
||
// Repro from #50519 | ||
|
||
type NonNullableNew<T> = T & {}; | ||
>NonNullableNew : NonNullableNew<T> | ||
|
||
type NonNullableOld<T> = T extends null | undefined ? never : T; | ||
>NonNullableOld : NonNullableOld<T> | ||
>null : null | ||
|
||
type TestNew = NonNullableNew<null>; | ||
>TestNew : never | ||
>null : null | ||
|
||
type TestOld = NonNullableOld<null>; | ||
>TestOld : never | ||
>null : null | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// @strict: true, false | ||
|
||
// These should all resolve to never | ||
|
||
type T0 = NonNullable<null>; | ||
type T1 = NonNullable<undefined>; | ||
type T2 = null & {}; | ||
type T3 = undefined & {}; | ||
type T4 = null & undefined; | ||
type T6 = null & { a: string } & {}; | ||
|
||
// Repro from #50519 | ||
|
||
type NonNullableNew<T> = T & {}; | ||
type NonNullableOld<T> = T extends null | undefined ? never : T; | ||
|
||
type TestNew = NonNullableNew<null>; | ||
type TestOld = NonNullableOld<null>; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we try to delete this whole
!strictNullChecks
branch entirely? Or are you still fine withundefined & null
being weirdlyundefined
in non-strict butnever
in strict?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need that check for cases where the intersection is a singleton
null
orundefined
.undefined & null
is already resolved tonever
by an earlier check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yeah, duh, I know why we can just delete this branch - 15 lines up there's a
strictNullChecks
only branch that does this already for strict mode - we can just delete both thatstrictNullChecks
gate and this entire conditional.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, that branch also resolves
null & object
andnull & { foo: string }
tonever
, which we don't want to do in non-strictNullChecks mode.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But why not? If we're already adjusting
null & {}
, isn't that just a logical followup?null & {x}
seems even more suspect thannull & {}
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole premise of
null
andundefined
being in the domain of every type is suspect. But that's non-strictNullChecks. My only objective here is to better align with pre-4.8 behavior in a corner case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be open to seeing what happens and considering such a change for 4.9, but I think this workaround for 4.8 is okay for now.