-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Fixed an issue with apparent mapped type keys #57838
Open
Andarist
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
Andarist:fix/apparent-mapped-type-keys-with-unknown-modifier
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
5 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 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 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
106 changes: 106 additions & 0 deletions
106
tests/baselines/reference/keyRemappingKeyofResult(strict=false).errors.txt
This file contains 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,106 @@ | ||
keyRemappingKeyofResult.ts(82,3): error TS2322: Type 'string' is not assignable to type 'Extract<keyof { [P in keyof T as T[P] extends string ? P : never]: any; }, string>'. | ||
keyRemappingKeyofResult.ts(90,3): error TS2322: Type 'string' is not assignable to type 'keyof { [P in keyof T as T[P] extends string ? P : never]: any; }'. | ||
Type 'string' is not assignable to type 'T[P] extends string ? P : never'. | ||
|
||
|
||
==== keyRemappingKeyofResult.ts (2 errors) ==== | ||
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; | ||
z = "foo"; // error | ||
~ | ||
!!! error TS2322: Type 'string' is not assignable to type 'Extract<keyof { [P in keyof T as T[P] extends string ? P : never]: any; }, string>'. | ||
} | ||
|
||
type StringKeys2<T> = keyof { | ||
[P in keyof T as T[P] extends string ? P : never]: any; | ||
}; | ||
|
||
function h<T>(z: StringKeys2<T>) { | ||
z = "foo"; // error | ||
~ | ||
!!! error TS2322: Type 'string' is not assignable to type 'keyof { [P in keyof T as T[P] extends string ? P : never]: any; }'. | ||
!!! error TS2322: Type 'string' is not assignable to type 'T[P] extends string ? P : never'. | ||
const f: string = z; // ok | ||
} | ||
|
||
export {}; | ||
|
This file contains 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 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
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.
I find it a bit weird that:
keyof unknown
isundefined
.getApparentMappedTypeKeys
. I'd expect it to be insidegetApparentMappedTypeKeys
. Isn't there a case wheregetApparentMappedTypeKeys
isnever
andsourceMappedKeys
should stay asnever
?This doesn't look wrong to me, but I can't yet form a good mental model of what's happening, so I don't know what to think. 😅
I'll revisit in a few days and maybe I'll have a better idea of what this all means.
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.
If we think about
keyof T
as a set of types that can be safely used to access properties ofT
inT[keyof T]
thennever
makes sense here.never
is the only type that can be safely used askeyof T
after we instantiateT
asunknown
.That's what I started with but mapping
never
toPropertyKey
would break the other call site wheregetApparentMappedTypeKeys
is used. In there, it's used to get the target keys. So this would remove the error below: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 all has me thinking that the original code that uses
getApparentMappedTypeKeys
on the source side is just... wrong?