-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Fixed an issue with apparent mapped type keys #57838
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
Andarist
wants to merge
10
commits into
microsoft:main
from
Andarist:fix/apparent-mapped-type-keys-with-unknown-modifier
Closed
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1a27fd9
Fixed an issue with apparent mapped type keys
Andarist 8d18d2d
Merge remote-tracking branch 'origin/main' into fix/apparent-mapped-t…
Andarist 8c95b0e
Merge remote-tracking branch 'origin/main' into fix/apparent-mapped-t…
Andarist 04976bf
improve the fix
Andarist cad532a
add extra test case
Andarist 05cc6dd
Merge remote-tracking branch 'origin/main' into fix/apparent-mapped-t…
Andarist 21f4f7c
move the fix to `getApparentMappedTypeKeys`
Andarist fcdaa91
remove `getApparentMappedTypeKeys` on the source side
Andarist 81185ac
Merge remote-tracking branch 'origin/main' into fix/apparent-mapped-t…
Andarist 73d96e1
fix index type constraints on mapped types with name types
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
117 changes: 117 additions & 0 deletions
117
tests/baselines/reference/keyRemappingKeyofResult(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,117 @@ | ||
| //// [tests/cases/compiler/keyRemappingKeyofResult.ts] //// | ||
|
|
||
| //// [keyRemappingKeyofResult.ts] | ||
| const sym = Symbol("") | ||
| type Orig = { [k: string]: any, str: any, [sym]: any } | ||
|
|
||
| type Okay = Exclude<keyof Orig, never> | ||
| // type Okay = string | number | typeof sym | ||
|
|
||
| type Remapped = { [K in keyof Orig as {} extends Record<K, any> ? never : K]: any } | ||
| /* type Remapped = { | ||
| str: any; | ||
| [sym]: any; | ||
| } */ | ||
| // no string index signature, right? | ||
|
|
||
| type Oops = Exclude<keyof Remapped, never> | ||
| declare let x: Oops; | ||
| x = sym; | ||
| x = "str"; | ||
| // type Oops = typeof sym <-- what happened to "str"? | ||
|
|
||
| // equivalently, with an unresolved generic (no `exclude` shenanigans, since conditions won't execute): | ||
| function f<T>() { | ||
| type Orig = { [k: string]: any, str: any, [sym]: any } & T; | ||
|
|
||
| type Okay = keyof Orig; | ||
| let a: Okay; | ||
| a = "str"; | ||
| a = sym; | ||
| a = "whatever"; | ||
| // type Okay = string | number | typeof sym | ||
|
|
||
| type Remapped = { [K in keyof Orig as {} extends Record<K, any> ? never : K]: any } | ||
| /* type Remapped = { | ||
| str: any; | ||
| [sym]: any; | ||
| } */ | ||
| // no string index signature, right? | ||
|
|
||
| type Oops = keyof Remapped; | ||
| let x: Oops; | ||
| x = sym; | ||
| x = "str"; | ||
| } | ||
|
|
||
| // and another generic case with a _distributive_ mapping, to trigger a different branch in `getIndexType` | ||
| function g<T>() { | ||
| type Orig = { [k: string]: any, str: any, [sym]: any } & T; | ||
|
|
||
| type Okay = keyof Orig; | ||
| let a: Okay; | ||
| a = "str"; | ||
| a = sym; | ||
| a = "whatever"; | ||
| // type Okay = string | number | typeof sym | ||
|
|
||
| type NonIndex<T extends PropertyKey> = {} extends Record<T, any> ? never : T; | ||
| type DistributiveNonIndex<T extends PropertyKey> = T extends unknown ? NonIndex<T> : never; | ||
|
|
||
| type Remapped = { [K in keyof Orig as DistributiveNonIndex<K>]: any } | ||
| /* type Remapped = { | ||
| str: any; | ||
| [sym]: any; | ||
| } */ | ||
| // no string index signature, right? | ||
|
|
||
| type Oops = keyof Remapped; | ||
| let x: Oops; | ||
| x = sym; | ||
| x = "str"; | ||
| } | ||
|
|
||
| // https://github.com/microsoft/TypeScript/issues/57827 | ||
|
|
||
| type StringKeys<T> = Extract< | ||
| keyof { | ||
| [P in keyof T as T[P] extends string ? P : never]: any; | ||
| }, | ||
| string | ||
| >; | ||
|
|
||
| function test_57827<T>(z: StringKeys<T>) { | ||
| const f: string = z; | ||
| } | ||
|
|
||
| export {}; | ||
|
|
||
| //// [keyRemappingKeyofResult.js] | ||
| const sym = Symbol(""); | ||
| x = sym; | ||
| x = "str"; | ||
| // type Oops = typeof sym <-- what happened to "str"? | ||
| // equivalently, with an unresolved generic (no `exclude` shenanigans, since conditions won't execute): | ||
| function f() { | ||
| let a; | ||
| a = "str"; | ||
| a = sym; | ||
| a = "whatever"; | ||
| let x; | ||
| x = sym; | ||
| x = "str"; | ||
| } | ||
| // and another generic case with a _distributive_ mapping, to trigger a different branch in `getIndexType` | ||
| function g() { | ||
| let a; | ||
| a = "str"; | ||
| a = sym; | ||
| a = "whatever"; | ||
| let x; | ||
| x = sym; | ||
| x = "str"; | ||
| } | ||
| function test_57827(z) { | ||
| const f = z; | ||
| } | ||
| export {}; |
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.
Uh oh!
There was an error while loading. Please reload this page.
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 problem here is that when
modifiersTypeisunknown(or{}) thenkeyof modifiersTypeisnever. Sinceneveris assignable to everything, the checker assumed thatkeyof { [P in keyof T as T[P] extends string ? P: never]: any }is assignable tostring.Then based on that information it managed to simplify the instantiated substitution type of
Extractto justkeyof { [P in keyof T as T[P] extends string ? P: never]: any }(itsnewBaseType). Then in turngetNarrowableTypeForReferenceconcluded that it should substitute constraints. Those were substituted withstringNumberSymbolType(for thekeyof ...) and that's not assignable tostring.Before #56742 , it didn't mistakenly assume that
keyof { [P in keyof T as T[P] extends string ? P: never]: any }is assignable tostringso the substitution type was instantiated as(keyof { [P in keyof T as T[P] extends string ? P: never]: any }) & stringand that is assignable to a string.