-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fixed an issue with optionality not being stripped from generic mapped types on indexed access #59921
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
1
commit into
microsoft:main
Choose a base branch
from
Andarist:fix/generic-mapped-type-optionality-stripped
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
Fixed an issue with optionality not being stripped from generic mapped types on indexed access #59921
Changes from all commits
Commits
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
20 changes: 20 additions & 0 deletions
20
tests/baselines/reference/genericMappedTypeOptionalityModifier1.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 @@ | ||
genericMappedTypeOptionalityModifier1.ts(10,9): error TS2322: Type 'undefined' is not assignable to type 'RequiredValues'. | ||
|
||
|
||
==== genericMappedTypeOptionalityModifier1.ts (1 errors) ==== | ||
// https://github.com/microsoft/TypeScript/issues/59902 | ||
|
||
const example = <T extends string>() => { | ||
type SomeOptional = { [key in T]?: 1 }; | ||
|
||
type AllRequired = { [key in keyof SomeOptional]-?: 1 }; | ||
|
||
type RequiredValues = AllRequired[keyof AllRequired]; | ||
|
||
const x: RequiredValues = undefined; // error | ||
~ | ||
!!! error TS2322: Type 'undefined' is not assignable to type 'RequiredValues'. | ||
|
||
return x; | ||
}; | ||
|
34 changes: 34 additions & 0 deletions
34
tests/baselines/reference/genericMappedTypeOptionalityModifier1.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,34 @@ | ||
//// [tests/cases/compiler/genericMappedTypeOptionalityModifier1.ts] //// | ||
|
||
=== genericMappedTypeOptionalityModifier1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/59902 | ||
|
||
const example = <T extends string>() => { | ||
>example : Symbol(example, Decl(genericMappedTypeOptionalityModifier1.ts, 2, 5)) | ||
>T : Symbol(T, Decl(genericMappedTypeOptionalityModifier1.ts, 2, 17)) | ||
|
||
type SomeOptional = { [key in T]?: 1 }; | ||
>SomeOptional : Symbol(SomeOptional, Decl(genericMappedTypeOptionalityModifier1.ts, 2, 41)) | ||
>key : Symbol(key, Decl(genericMappedTypeOptionalityModifier1.ts, 3, 25)) | ||
>T : Symbol(T, Decl(genericMappedTypeOptionalityModifier1.ts, 2, 17)) | ||
|
||
type AllRequired = { [key in keyof SomeOptional]-?: 1 }; | ||
>AllRequired : Symbol(AllRequired, Decl(genericMappedTypeOptionalityModifier1.ts, 3, 41)) | ||
>key : Symbol(key, Decl(genericMappedTypeOptionalityModifier1.ts, 5, 24)) | ||
>SomeOptional : Symbol(SomeOptional, Decl(genericMappedTypeOptionalityModifier1.ts, 2, 41)) | ||
|
||
type RequiredValues = AllRequired[keyof AllRequired]; | ||
>RequiredValues : Symbol(RequiredValues, Decl(genericMappedTypeOptionalityModifier1.ts, 5, 58)) | ||
>AllRequired : Symbol(AllRequired, Decl(genericMappedTypeOptionalityModifier1.ts, 3, 41)) | ||
>AllRequired : Symbol(AllRequired, Decl(genericMappedTypeOptionalityModifier1.ts, 3, 41)) | ||
|
||
const x: RequiredValues = undefined; // error | ||
>x : Symbol(x, Decl(genericMappedTypeOptionalityModifier1.ts, 9, 7)) | ||
>RequiredValues : Symbol(RequiredValues, Decl(genericMappedTypeOptionalityModifier1.ts, 5, 58)) | ||
>undefined : Symbol(undefined) | ||
|
||
return x; | ||
>x : Symbol(x, Decl(genericMappedTypeOptionalityModifier1.ts, 9, 7)) | ||
|
||
}; | ||
|
35 changes: 35 additions & 0 deletions
35
tests/baselines/reference/genericMappedTypeOptionalityModifier1.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,35 @@ | ||
//// [tests/cases/compiler/genericMappedTypeOptionalityModifier1.ts] //// | ||
|
||
=== genericMappedTypeOptionalityModifier1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/59902 | ||
|
||
const example = <T extends string>() => { | ||
>example : <T extends string>() => { [key_1 in keyof { [key in T]?: 1 | undefined; }]-?: 1; }[T] | ||
> : ^ ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
><T extends string>() => { type SomeOptional = { [key in T]?: 1 }; type AllRequired = { [key in keyof SomeOptional]-?: 1 }; type RequiredValues = AllRequired[keyof AllRequired]; const x: RequiredValues = undefined; // error return x;} : <T extends string>() => { [key_1 in keyof { [key in T]?: 1 | undefined; }]-?: 1; }[T] | ||
> : ^ ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
type SomeOptional = { [key in T]?: 1 }; | ||
>SomeOptional : { [key in T]?: 1 | undefined; } | ||
> : ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
type AllRequired = { [key in keyof SomeOptional]-?: 1 }; | ||
>AllRequired : { [key_1 in keyof { [key in T]?: 1 | undefined; }]-?: 1; } | ||
> : ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
type RequiredValues = AllRequired[keyof AllRequired]; | ||
>RequiredValues : { [key_1 in keyof { [key in T]?: 1 | undefined; }]-?: 1; }[T] | ||
> : ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
const x: RequiredValues = undefined; // error | ||
>x : { [key_1 in keyof { [key in T]?: 1 | undefined; }]-?: 1; }[T] | ||
> : ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>undefined : undefined | ||
> : ^^^^^^^^^ | ||
|
||
return x; | ||
>x : { [key_1 in keyof { [key in T]?: 1 | undefined; }]-?: 1; }[T] | ||
> : ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
}; | ||
|
16 changes: 16 additions & 0 deletions
16
tests/cases/compiler/genericMappedTypeOptionalityModifier1.ts
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,16 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
// https://github.com/microsoft/TypeScript/issues/59902 | ||
|
||
const example = <T extends string>() => { | ||
type SomeOptional = { [key in T]?: 1 }; | ||
|
||
type AllRequired = { [key in keyof SomeOptional]-?: 1 }; | ||
|
||
type RequiredValues = AllRequired[keyof AllRequired]; | ||
|
||
const x: RequiredValues = undefined; // error | ||
|
||
return x; | ||
}; |
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.
In the given repro the immediate modifiers type has
-?
for whichgetMappedTypeOptionality
returns-1
. So the first condition was not satisfied and the compiler proceeded to computegetCombinedMappedTypeOptionality
. This time it was called with the modifiers type and not the original object type. Since that was a mapped type too it got its modifiers type etc.Essentially this was skipping the outermost mapped type modifiers in this scenario - the ones that were supposed strip the optionality.