-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Remove missing type from tuple type arguments under exactOptionalPropertyTypes
#54718
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
8
commits into
microsoft:main
Choose a base branch
from
Andarist:fix/for-of-iteration-optional-tuple-members
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.
+1,090
−66
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f43c8d
Remove missing type from tuple type arguments under `exactOptionalPro…
Andarist 64843f4
Merge remote-tracking branch 'origin/main' into fix/for-of-iteration-…
Andarist cd8f59c
avoid adding a missing type instead of removing it afterward
Andarist 31d5bf7
Fixed more cases
Andarist 3407b6b
Remove redundant `removeMissingType` calls
Andarist 3c846b4
Revert part of the `removeMissingType` and add a test for it
Andarist 0682323
Add extra test case
Andarist 486c327
Merge remote-tracking branch 'origin/main' into fix/for-of-iteration-…
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
Some comments aren't visible on the classic Files Changed page.
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
20 changes: 20 additions & 0 deletions
20
...eference/forOfOptionalTupleMember(exactoptionalpropertytypes=false,target=es5).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,20 @@ | ||
| forOfOptionalTupleMember.ts(12,3): error TS18048: 'item' is possibly 'undefined'. | ||
|
|
||
|
|
||
| ==== forOfOptionalTupleMember.ts (1 errors) ==== | ||
| // repro from https://github.com/microsoft/TypeScript/issues/54302 | ||
|
|
||
| type Item = { | ||
| value: string; | ||
| }; | ||
|
|
||
| type Foo = [Item?]; | ||
|
|
||
| declare const foo: Foo; | ||
|
|
||
| for (let item of foo) { | ||
| item.value; | ||
| ~~~~ | ||
| !!! error TS18048: 'item' is possibly 'undefined'. | ||
| } | ||
|
|
31 changes: 31 additions & 0 deletions
31
...s/reference/forOfOptionalTupleMember(exactoptionalpropertytypes=false,target=es5).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,31 @@ | ||
| //// [tests/cases/compiler/forOfOptionalTupleMember.ts] //// | ||
|
|
||
| === forOfOptionalTupleMember.ts === | ||
| // repro from https://github.com/microsoft/TypeScript/issues/54302 | ||
|
|
||
| type Item = { | ||
| >Item : Symbol(Item, Decl(forOfOptionalTupleMember.ts, 0, 0)) | ||
|
|
||
| value: string; | ||
| >value : Symbol(value, Decl(forOfOptionalTupleMember.ts, 2, 13)) | ||
|
|
||
| }; | ||
|
|
||
| type Foo = [Item?]; | ||
| >Foo : Symbol(Foo, Decl(forOfOptionalTupleMember.ts, 4, 2)) | ||
| >Item : Symbol(Item, Decl(forOfOptionalTupleMember.ts, 0, 0)) | ||
|
|
||
| declare const foo: Foo; | ||
| >foo : Symbol(foo, Decl(forOfOptionalTupleMember.ts, 8, 13)) | ||
| >Foo : Symbol(Foo, Decl(forOfOptionalTupleMember.ts, 4, 2)) | ||
|
|
||
| for (let item of foo) { | ||
| >item : Symbol(item, Decl(forOfOptionalTupleMember.ts, 10, 8)) | ||
| >foo : Symbol(foo, Decl(forOfOptionalTupleMember.ts, 8, 13)) | ||
|
|
||
| item.value; | ||
| >item.value : Symbol(value, Decl(forOfOptionalTupleMember.ts, 2, 13)) | ||
| >item : Symbol(item, Decl(forOfOptionalTupleMember.ts, 10, 8)) | ||
| >value : Symbol(value, Decl(forOfOptionalTupleMember.ts, 2, 13)) | ||
| } | ||
|
|
29 changes: 29 additions & 0 deletions
29
...nes/reference/forOfOptionalTupleMember(exactoptionalpropertytypes=false,target=es5).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,29 @@ | ||
| //// [tests/cases/compiler/forOfOptionalTupleMember.ts] //// | ||
|
|
||
| === forOfOptionalTupleMember.ts === | ||
| // repro from https://github.com/microsoft/TypeScript/issues/54302 | ||
|
|
||
| type Item = { | ||
| >Item : { value: string; } | ||
|
|
||
| value: string; | ||
| >value : string | ||
|
|
||
| }; | ||
|
|
||
| type Foo = [Item?]; | ||
| >Foo : [(Item | undefined)?] | ||
|
|
||
| declare const foo: Foo; | ||
| >foo : Foo | ||
|
|
||
| for (let item of foo) { | ||
| >item : Item | undefined | ||
| >foo : Foo | ||
|
|
||
| item.value; | ||
| >item.value : string | ||
| >item : Item | undefined | ||
| >value : string | ||
| } | ||
|
|
20 changes: 20 additions & 0 deletions
20
...rence/forOfOptionalTupleMember(exactoptionalpropertytypes=false,target=esnext).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,20 @@ | ||
| forOfOptionalTupleMember.ts(12,3): error TS18048: 'item' is possibly 'undefined'. | ||
|
|
||
|
|
||
| ==== forOfOptionalTupleMember.ts (1 errors) ==== | ||
| // repro from https://github.com/microsoft/TypeScript/issues/54302 | ||
|
|
||
| type Item = { | ||
| value: string; | ||
| }; | ||
|
|
||
| type Foo = [Item?]; | ||
|
|
||
| declare const foo: Foo; | ||
|
|
||
| for (let item of foo) { | ||
| item.value; | ||
| ~~~~ | ||
| !!! error TS18048: 'item' is possibly 'undefined'. | ||
| } | ||
|
|
31 changes: 31 additions & 0 deletions
31
...eference/forOfOptionalTupleMember(exactoptionalpropertytypes=false,target=esnext).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,31 @@ | ||
| //// [tests/cases/compiler/forOfOptionalTupleMember.ts] //// | ||
|
|
||
| === forOfOptionalTupleMember.ts === | ||
| // repro from https://github.com/microsoft/TypeScript/issues/54302 | ||
|
|
||
| type Item = { | ||
| >Item : Symbol(Item, Decl(forOfOptionalTupleMember.ts, 0, 0)) | ||
|
|
||
| value: string; | ||
| >value : Symbol(value, Decl(forOfOptionalTupleMember.ts, 2, 13)) | ||
|
|
||
| }; | ||
|
|
||
| type Foo = [Item?]; | ||
| >Foo : Symbol(Foo, Decl(forOfOptionalTupleMember.ts, 4, 2)) | ||
| >Item : Symbol(Item, Decl(forOfOptionalTupleMember.ts, 0, 0)) | ||
|
|
||
| declare const foo: Foo; | ||
| >foo : Symbol(foo, Decl(forOfOptionalTupleMember.ts, 8, 13)) | ||
| >Foo : Symbol(Foo, Decl(forOfOptionalTupleMember.ts, 4, 2)) | ||
|
|
||
| for (let item of foo) { | ||
| >item : Symbol(item, Decl(forOfOptionalTupleMember.ts, 10, 8)) | ||
| >foo : Symbol(foo, Decl(forOfOptionalTupleMember.ts, 8, 13)) | ||
|
|
||
| item.value; | ||
| >item.value : Symbol(value, Decl(forOfOptionalTupleMember.ts, 2, 13)) | ||
| >item : Symbol(item, Decl(forOfOptionalTupleMember.ts, 10, 8)) | ||
| >value : Symbol(value, Decl(forOfOptionalTupleMember.ts, 2, 13)) | ||
| } | ||
|
|
29 changes: 29 additions & 0 deletions
29
.../reference/forOfOptionalTupleMember(exactoptionalpropertytypes=false,target=esnext).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,29 @@ | ||
| //// [tests/cases/compiler/forOfOptionalTupleMember.ts] //// | ||
|
|
||
| === forOfOptionalTupleMember.ts === | ||
| // repro from https://github.com/microsoft/TypeScript/issues/54302 | ||
|
|
||
| type Item = { | ||
| >Item : { value: string; } | ||
|
|
||
| value: string; | ||
| >value : string | ||
|
|
||
| }; | ||
|
|
||
| type Foo = [Item?]; | ||
| >Foo : [(Item | undefined)?] | ||
|
|
||
| declare const foo: Foo; | ||
| >foo : Foo | ||
|
|
||
| for (let item of foo) { | ||
| >item : Item | undefined | ||
| >foo : Foo | ||
|
|
||
| item.value; | ||
| >item.value : string | ||
| >item : Item | undefined | ||
| >value : string | ||
| } | ||
|
|
31 changes: 31 additions & 0 deletions
31
...es/reference/forOfOptionalTupleMember(exactoptionalpropertytypes=true,target=es5).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,31 @@ | ||
| //// [tests/cases/compiler/forOfOptionalTupleMember.ts] //// | ||
|
|
||
| === forOfOptionalTupleMember.ts === | ||
| // repro from https://github.com/microsoft/TypeScript/issues/54302 | ||
|
|
||
| type Item = { | ||
| >Item : Symbol(Item, Decl(forOfOptionalTupleMember.ts, 0, 0)) | ||
|
|
||
| value: string; | ||
| >value : Symbol(value, Decl(forOfOptionalTupleMember.ts, 2, 13)) | ||
|
|
||
| }; | ||
|
|
||
| type Foo = [Item?]; | ||
| >Foo : Symbol(Foo, Decl(forOfOptionalTupleMember.ts, 4, 2)) | ||
| >Item : Symbol(Item, Decl(forOfOptionalTupleMember.ts, 0, 0)) | ||
|
|
||
| declare const foo: Foo; | ||
| >foo : Symbol(foo, Decl(forOfOptionalTupleMember.ts, 8, 13)) | ||
| >Foo : Symbol(Foo, Decl(forOfOptionalTupleMember.ts, 4, 2)) | ||
|
|
||
| for (let item of foo) { | ||
| >item : Symbol(item, Decl(forOfOptionalTupleMember.ts, 10, 8)) | ||
| >foo : Symbol(foo, Decl(forOfOptionalTupleMember.ts, 8, 13)) | ||
|
|
||
| item.value; | ||
| >item.value : Symbol(value, Decl(forOfOptionalTupleMember.ts, 2, 13)) | ||
| >item : Symbol(item, Decl(forOfOptionalTupleMember.ts, 10, 8)) | ||
| >value : Symbol(value, Decl(forOfOptionalTupleMember.ts, 2, 13)) | ||
| } | ||
|
|
29 changes: 29 additions & 0 deletions
29
...ines/reference/forOfOptionalTupleMember(exactoptionalpropertytypes=true,target=es5).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,29 @@ | ||
| //// [tests/cases/compiler/forOfOptionalTupleMember.ts] //// | ||
|
|
||
| === forOfOptionalTupleMember.ts === | ||
| // repro from https://github.com/microsoft/TypeScript/issues/54302 | ||
|
|
||
| type Item = { | ||
| >Item : { value: string; } | ||
|
|
||
| value: string; | ||
| >value : string | ||
|
|
||
| }; | ||
|
|
||
| type Foo = [Item?]; | ||
| >Foo : [Item?] | ||
|
|
||
| declare const foo: Foo; | ||
| >foo : Foo | ||
|
|
||
| for (let item of foo) { | ||
| >item : Item | ||
| >foo : Foo | ||
|
|
||
| item.value; | ||
| >item.value : string | ||
| >item : Item | ||
| >value : string | ||
| } | ||
|
|
31 changes: 31 additions & 0 deletions
31
...reference/forOfOptionalTupleMember(exactoptionalpropertytypes=true,target=esnext).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,31 @@ | ||
| //// [tests/cases/compiler/forOfOptionalTupleMember.ts] //// | ||
|
|
||
| === forOfOptionalTupleMember.ts === | ||
| // repro from https://github.com/microsoft/TypeScript/issues/54302 | ||
|
|
||
| type Item = { | ||
| >Item : Symbol(Item, Decl(forOfOptionalTupleMember.ts, 0, 0)) | ||
|
|
||
| value: string; | ||
| >value : Symbol(value, Decl(forOfOptionalTupleMember.ts, 2, 13)) | ||
|
|
||
| }; | ||
|
|
||
| type Foo = [Item?]; | ||
| >Foo : Symbol(Foo, Decl(forOfOptionalTupleMember.ts, 4, 2)) | ||
| >Item : Symbol(Item, Decl(forOfOptionalTupleMember.ts, 0, 0)) | ||
|
|
||
| declare const foo: Foo; | ||
| >foo : Symbol(foo, Decl(forOfOptionalTupleMember.ts, 8, 13)) | ||
| >Foo : Symbol(Foo, Decl(forOfOptionalTupleMember.ts, 4, 2)) | ||
|
|
||
| for (let item of foo) { | ||
| >item : Symbol(item, Decl(forOfOptionalTupleMember.ts, 10, 8)) | ||
| >foo : Symbol(foo, Decl(forOfOptionalTupleMember.ts, 8, 13)) | ||
|
|
||
| item.value; | ||
| >item.value : Symbol(value, Decl(forOfOptionalTupleMember.ts, 2, 13)) | ||
| >item : Symbol(item, Decl(forOfOptionalTupleMember.ts, 10, 8)) | ||
| >value : Symbol(value, Decl(forOfOptionalTupleMember.ts, 2, 13)) | ||
| } | ||
|
|
29 changes: 29 additions & 0 deletions
29
...s/reference/forOfOptionalTupleMember(exactoptionalpropertytypes=true,target=esnext).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,29 @@ | ||
| //// [tests/cases/compiler/forOfOptionalTupleMember.ts] //// | ||
|
|
||
| === forOfOptionalTupleMember.ts === | ||
| // repro from https://github.com/microsoft/TypeScript/issues/54302 | ||
|
|
||
| type Item = { | ||
| >Item : { value: string; } | ||
|
|
||
| value: string; | ||
| >value : string | ||
|
|
||
| }; | ||
|
|
||
| type Foo = [Item?]; | ||
| >Foo : [Item?] | ||
|
|
||
| declare const foo: Foo; | ||
| >foo : Foo | ||
|
|
||
| for (let item of foo) { | ||
| >item : Item | ||
| >foo : Foo | ||
|
|
||
| item.value; | ||
| >item.value : string | ||
| >item : Item | ||
| >value : string | ||
| } | ||
|
|
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 | ||
| // @target: es5, esnext | ||
| // @exactOptionalPropertyTypes: true, false | ||
| // @noEmit: true | ||
|
|
||
| // repro from https://github.com/microsoft/TypeScript/issues/54302 | ||
|
|
||
| type Item = { | ||
| value: string; | ||
| }; | ||
|
|
||
| type Foo = [Item?]; | ||
|
|
||
| declare const foo: Foo; | ||
|
|
||
| for (let item of foo) { | ||
| item.value; | ||
| } |
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.
It seems strange to me that this is being done inside of
getTypeArguments; previously this function effectively just pulled info from the node and that's it, no processing.Is there no other place this makes sense?
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 go even further and say that the optionality should always be removed from those type arguments (of optional elements).
When it comes to object properties the optionality is added to the declared type based on the optionality marker on the property, see here. I see a strong parallel between this and the element types. The type argument is like a property's declared type - the optionality is a separate piece of information.
In a somewhat similar manner, in the case of a rest element - the type argument is not an array type! It's an element type of that array and the final array type~ gets created when the type argument meets the element flag.
I think removing this from the type arguments is best because the iterated type of an array comes from the
Symbol.iteratormethod. Its signature is defined as(): IterableIterator<T>andTgets instantiated to the union that contains all type arguments. So at the end, we get something like(): IterableIterator<string | number | undefined>.We might notice here that the rest type fits into this perfectly because its type is not an array type - so if we just push that to the union we end up with its element type and not an array, its concatenable~ with required elements, and the
Symbol.iteratormethod that gets instantiated this way is correct.It doesn't work well with optional elements in combination with
exactOptionalPropertyTypesthough if theundefinedis included in the type argument right from the start. After all, at runtime - we won't actually iterate over thatundefinedsinceundefinedisn't assignable to an optional element under EOPT.So, I run an experiment to remove this at all times and "add back" that optionality at other places (see the commit here). It turned out to be... semi-successful. All the tests pass except the one that I'm trying to fix here.
The problem with this is that the easiest way to add back the optionality is through the created mapper (here). But then the
T -> UnionOfTypeArgumentsuses that as well when instantiating theSymbol.iteratormethod. I don't see a clean way to branch this somehow or to remove the missing type afterward. After all, it's not only about what we get internally. TheSymbol.iterator's method should get instantiated to, let's say,(): IterableIterator<string | number>and not to(): IterableIterator<string | number | undefined>. That's publicly~ visible information that can be obtained through types in user programs.So, for better or worse - I think that it's best to leave things as-is but avoid adding the
missingTypeto optional type arguments under EOPT. Perhaps a cleaner way of doing this would be something along those lines:I think I like this one better and I am going to push it out in a second.
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.
There is also a non-zero chance that this actually makes some of the existing
removeMissingTypecalls redundant. I will investigate this.