-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Error on destructured nullables #60121
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
Open
Andarist
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
Andarist:fix/error-on-destructured-nullables
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
28cdebc
Error on destructured nullables
Andarist 282ee86
fixed issue with nullable parent types in binding patterns
Andarist df673cb
Merge remote-tracking branch 'origin/main' into fix/error-on-destruct…
Andarist c0ef609
add extra test case
Andarist ffc55f4
Merge remote-tracking branch 'origin/main' into fix/error-on-destruct…
Andarist fff34bc
add extra test
Andarist 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
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
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
50 changes: 50 additions & 0 deletions
50
tests/baselines/reference/bindingPatternWithNullableInitializer1.errors.txt
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,50 @@ | ||
| bindingPatternWithNullableInitializer1.ts(10,22): error TS18048: 't' is possibly 'undefined'. | ||
| bindingPatternWithNullableInitializer1.ts(14,22): error TS18047: 't' is possibly 'null'. | ||
| bindingPatternWithNullableInitializer1.ts(18,22): error TS18049: 't' is possibly 'null' or 'undefined'. | ||
| bindingPatternWithNullableInitializer1.ts(22,14): error TS2532: Object is possibly 'undefined'. | ||
| bindingPatternWithNullableInitializer1.ts(31,18): error TS18048: 'input.a.b' is possibly 'undefined'. | ||
|
|
||
|
|
||
| ==== bindingPatternWithNullableInitializer1.ts (5 errors) ==== | ||
| // https://github.com/microsoft/TypeScript/issues/60119 | ||
|
|
||
| interface T { | ||
| a?: unknown; | ||
| b?: unknown; | ||
| c?: unknown; | ||
| } | ||
|
|
||
| function f1<L extends keyof T>(k: L, t: T | undefined) { | ||
| const { [k]: v } = t; | ||
| ~ | ||
| !!! error TS18048: 't' is possibly 'undefined'. | ||
| } | ||
|
|
||
| function f2<L extends keyof T>(k: L, t: T | null) { | ||
| const { [k]: v } = t; | ||
| ~ | ||
| !!! error TS18047: 't' is possibly 'null'. | ||
| } | ||
|
|
||
| function f3<L extends keyof T>(k: L, t: T | null | undefined) { | ||
| const { [k]: v } = t; | ||
| ~ | ||
| !!! error TS18049: 't' is possibly 'null' or 'undefined'. | ||
| } | ||
|
|
||
| function f4<L extends keyof T>(k: L, t: { f: T | undefined }) { | ||
| const { f: { [k]: v } } = t; | ||
| ~~~~~~~~~~ | ||
| !!! error TS2532: Object is possibly 'undefined'. | ||
| } | ||
|
|
||
| // https://github.com/microsoft/TypeScript/issues/60179 | ||
|
|
||
| const input: { | ||
| a?: { b?: { c: string } }; | ||
| } = { a: undefined }; | ||
|
|
||
| const { ...b } = input.a?.b; | ||
| ~~~~~~~~~~ | ||
| !!! error TS18048: 'input.a.b' is possibly 'undefined'. | ||
|
|
102 changes: 102 additions & 0 deletions
102
tests/baselines/reference/bindingPatternWithNullableInitializer1.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,102 @@ | ||
| //// [tests/cases/conformance/es6/destructuring/bindingPatternWithNullableInitializer1.ts] //// | ||
|
|
||
| === bindingPatternWithNullableInitializer1.ts === | ||
| // https://github.com/microsoft/TypeScript/issues/60119 | ||
|
|
||
| interface T { | ||
| >T : Symbol(T, Decl(bindingPatternWithNullableInitializer1.ts, 0, 0)) | ||
|
|
||
| a?: unknown; | ||
| >a : Symbol(T.a, Decl(bindingPatternWithNullableInitializer1.ts, 2, 13)) | ||
|
|
||
| b?: unknown; | ||
| >b : Symbol(T.b, Decl(bindingPatternWithNullableInitializer1.ts, 3, 14)) | ||
|
|
||
| c?: unknown; | ||
| >c : Symbol(T.c, Decl(bindingPatternWithNullableInitializer1.ts, 4, 14)) | ||
| } | ||
|
|
||
| function f1<L extends keyof T>(k: L, t: T | undefined) { | ||
| >f1 : Symbol(f1, Decl(bindingPatternWithNullableInitializer1.ts, 6, 1)) | ||
| >L : Symbol(L, Decl(bindingPatternWithNullableInitializer1.ts, 8, 12)) | ||
| >T : Symbol(T, Decl(bindingPatternWithNullableInitializer1.ts, 0, 0)) | ||
| >k : Symbol(k, Decl(bindingPatternWithNullableInitializer1.ts, 8, 31)) | ||
| >L : Symbol(L, Decl(bindingPatternWithNullableInitializer1.ts, 8, 12)) | ||
| >t : Symbol(t, Decl(bindingPatternWithNullableInitializer1.ts, 8, 36)) | ||
| >T : Symbol(T, Decl(bindingPatternWithNullableInitializer1.ts, 0, 0)) | ||
|
|
||
| const { [k]: v } = t; | ||
| >k : Symbol(k, Decl(bindingPatternWithNullableInitializer1.ts, 8, 31)) | ||
| >v : Symbol(v, Decl(bindingPatternWithNullableInitializer1.ts, 9, 9)) | ||
| >t : Symbol(t, Decl(bindingPatternWithNullableInitializer1.ts, 8, 36)) | ||
| } | ||
|
|
||
| function f2<L extends keyof T>(k: L, t: T | null) { | ||
| >f2 : Symbol(f2, Decl(bindingPatternWithNullableInitializer1.ts, 10, 1)) | ||
| >L : Symbol(L, Decl(bindingPatternWithNullableInitializer1.ts, 12, 12)) | ||
| >T : Symbol(T, Decl(bindingPatternWithNullableInitializer1.ts, 0, 0)) | ||
| >k : Symbol(k, Decl(bindingPatternWithNullableInitializer1.ts, 12, 31)) | ||
| >L : Symbol(L, Decl(bindingPatternWithNullableInitializer1.ts, 12, 12)) | ||
| >t : Symbol(t, Decl(bindingPatternWithNullableInitializer1.ts, 12, 36)) | ||
| >T : Symbol(T, Decl(bindingPatternWithNullableInitializer1.ts, 0, 0)) | ||
|
|
||
| const { [k]: v } = t; | ||
| >k : Symbol(k, Decl(bindingPatternWithNullableInitializer1.ts, 12, 31)) | ||
| >v : Symbol(v, Decl(bindingPatternWithNullableInitializer1.ts, 13, 9)) | ||
| >t : Symbol(t, Decl(bindingPatternWithNullableInitializer1.ts, 12, 36)) | ||
| } | ||
|
|
||
| function f3<L extends keyof T>(k: L, t: T | null | undefined) { | ||
| >f3 : Symbol(f3, Decl(bindingPatternWithNullableInitializer1.ts, 14, 1)) | ||
| >L : Symbol(L, Decl(bindingPatternWithNullableInitializer1.ts, 16, 12)) | ||
| >T : Symbol(T, Decl(bindingPatternWithNullableInitializer1.ts, 0, 0)) | ||
| >k : Symbol(k, Decl(bindingPatternWithNullableInitializer1.ts, 16, 31)) | ||
| >L : Symbol(L, Decl(bindingPatternWithNullableInitializer1.ts, 16, 12)) | ||
| >t : Symbol(t, Decl(bindingPatternWithNullableInitializer1.ts, 16, 36)) | ||
| >T : Symbol(T, Decl(bindingPatternWithNullableInitializer1.ts, 0, 0)) | ||
|
|
||
| const { [k]: v } = t; | ||
| >k : Symbol(k, Decl(bindingPatternWithNullableInitializer1.ts, 16, 31)) | ||
| >v : Symbol(v, Decl(bindingPatternWithNullableInitializer1.ts, 17, 9)) | ||
| >t : Symbol(t, Decl(bindingPatternWithNullableInitializer1.ts, 16, 36)) | ||
| } | ||
|
|
||
| function f4<L extends keyof T>(k: L, t: { f: T | undefined }) { | ||
| >f4 : Symbol(f4, Decl(bindingPatternWithNullableInitializer1.ts, 18, 1)) | ||
| >L : Symbol(L, Decl(bindingPatternWithNullableInitializer1.ts, 20, 12)) | ||
| >T : Symbol(T, Decl(bindingPatternWithNullableInitializer1.ts, 0, 0)) | ||
| >k : Symbol(k, Decl(bindingPatternWithNullableInitializer1.ts, 20, 31)) | ||
| >L : Symbol(L, Decl(bindingPatternWithNullableInitializer1.ts, 20, 12)) | ||
| >t : Symbol(t, Decl(bindingPatternWithNullableInitializer1.ts, 20, 36)) | ||
| >f : Symbol(f, Decl(bindingPatternWithNullableInitializer1.ts, 20, 41)) | ||
| >T : Symbol(T, Decl(bindingPatternWithNullableInitializer1.ts, 0, 0)) | ||
|
|
||
| const { f: { [k]: v } } = t; | ||
| >f : Symbol(f, Decl(bindingPatternWithNullableInitializer1.ts, 20, 41)) | ||
| >k : Symbol(k, Decl(bindingPatternWithNullableInitializer1.ts, 20, 31)) | ||
| >v : Symbol(v, Decl(bindingPatternWithNullableInitializer1.ts, 21, 14)) | ||
| >t : Symbol(t, Decl(bindingPatternWithNullableInitializer1.ts, 20, 36)) | ||
| } | ||
|
|
||
| // https://github.com/microsoft/TypeScript/issues/60179 | ||
|
|
||
| const input: { | ||
| >input : Symbol(input, Decl(bindingPatternWithNullableInitializer1.ts, 26, 5)) | ||
|
|
||
| a?: { b?: { c: string } }; | ||
| >a : Symbol(a, Decl(bindingPatternWithNullableInitializer1.ts, 26, 14)) | ||
| >b : Symbol(b, Decl(bindingPatternWithNullableInitializer1.ts, 27, 7)) | ||
| >c : Symbol(c, Decl(bindingPatternWithNullableInitializer1.ts, 27, 13)) | ||
|
|
||
| } = { a: undefined }; | ||
| >a : Symbol(a, Decl(bindingPatternWithNullableInitializer1.ts, 28, 5)) | ||
| >undefined : Symbol(undefined) | ||
|
|
||
| const { ...b } = input.a?.b; | ||
| >b : Symbol(b, Decl(bindingPatternWithNullableInitializer1.ts, 30, 7)) | ||
| >input.a?.b : Symbol(b, Decl(bindingPatternWithNullableInitializer1.ts, 27, 7)) | ||
| >input.a : Symbol(a, Decl(bindingPatternWithNullableInitializer1.ts, 26, 14)) | ||
| >input : Symbol(input, Decl(bindingPatternWithNullableInitializer1.ts, 26, 5)) | ||
| >a : Symbol(a, Decl(bindingPatternWithNullableInitializer1.ts, 26, 14)) | ||
| >b : Symbol(b, Decl(bindingPatternWithNullableInitializer1.ts, 27, 7)) | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
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.
This gets a little bit noisy because we essentially report the same problem twice.
There are cases in the changed baselines that require the error to be reported on the initializer, like here:
TypeScript/tests/baselines/reference/destructuringArrayBindingPatternAndAssignment1ES5.errors.txt
Lines 32 to 37 in 28cdebc
So if there is desire to limit the amount of reported errors I think it would be good to report consistently them on the initializer at all times. I think handling this differently for different kind of patterns will complicate the code. But if there is a strong belief that this would provide a better DX - I can implement the required changes.