-
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
Improve relationship checking with keyof GenericMappedType
as the source type
#56246
Closed
Andarist
wants to merge
7
commits into
microsoft:main
from
Andarist:instantiate-name-type-of-filtering-mapped-types-for-keyof
+362
−12
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b9a01cb
Instantiate `nameType` of filtering mapped types when creating index …
Andarist e13ad38
just always instantiate the name type
Andarist 880d4af
Merge remote-tracking branch 'origin/main' into instantiate-name-type…
Andarist 4c69959
Revert `nameType` intantiation
Andarist 585091e
Improve relationship checking
Andarist ac23642
Reuse the logic in `getGenericMappedTypeKeys`
Andarist 3d1fe73
Merge remote-tracking branch 'origin/main' into instantiate-name-type…
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
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
84 changes: 84 additions & 0 deletions
84
tests/baselines/reference/keyRemappingKeyofResult2.symbols
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,84 @@ | ||
//// [tests/cases/compiler/keyRemappingKeyofResult2.ts] //// | ||
|
||
=== keyRemappingKeyofResult2.ts === | ||
// https://github.com/microsoft/TypeScript/issues/56239 | ||
|
||
type Values<T> = T[keyof T]; | ||
>Values : Symbol(Values, Decl(keyRemappingKeyofResult2.ts, 0, 0)) | ||
>T : Symbol(T, Decl(keyRemappingKeyofResult2.ts, 2, 12)) | ||
>T : Symbol(T, Decl(keyRemappingKeyofResult2.ts, 2, 12)) | ||
>T : Symbol(T, Decl(keyRemappingKeyofResult2.ts, 2, 12)) | ||
|
||
type ProvidedActor = { | ||
>ProvidedActor : Symbol(ProvidedActor, Decl(keyRemappingKeyofResult2.ts, 2, 28)) | ||
|
||
src: string; | ||
>src : Symbol(src, Decl(keyRemappingKeyofResult2.ts, 4, 22)) | ||
|
||
logic: unknown; | ||
>logic : Symbol(logic, Decl(keyRemappingKeyofResult2.ts, 5, 14)) | ||
|
||
}; | ||
|
||
interface StateMachineConfig<TActors extends ProvidedActor> { | ||
>StateMachineConfig : Symbol(StateMachineConfig, Decl(keyRemappingKeyofResult2.ts, 7, 2)) | ||
>TActors : Symbol(TActors, Decl(keyRemappingKeyofResult2.ts, 9, 29)) | ||
>ProvidedActor : Symbol(ProvidedActor, Decl(keyRemappingKeyofResult2.ts, 2, 28)) | ||
|
||
invoke: { | ||
>invoke : Symbol(StateMachineConfig.invoke, Decl(keyRemappingKeyofResult2.ts, 9, 61)) | ||
|
||
src: TActors["src"]; | ||
>src : Symbol(src, Decl(keyRemappingKeyofResult2.ts, 10, 11)) | ||
>TActors : Symbol(TActors, Decl(keyRemappingKeyofResult2.ts, 9, 29)) | ||
|
||
}; | ||
} | ||
|
||
declare function setup<TActors extends Record<string, unknown>>(_: { | ||
>setup : Symbol(setup, Decl(keyRemappingKeyofResult2.ts, 13, 1)) | ||
>TActors : Symbol(TActors, Decl(keyRemappingKeyofResult2.ts, 15, 23)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
>_ : Symbol(_, Decl(keyRemappingKeyofResult2.ts, 15, 64)) | ||
|
||
actors: { | ||
>actors : Symbol(actors, Decl(keyRemappingKeyofResult2.ts, 15, 68)) | ||
|
||
[K in keyof TActors]: TActors[K]; | ||
>K : Symbol(K, Decl(keyRemappingKeyofResult2.ts, 17, 5)) | ||
>TActors : Symbol(TActors, Decl(keyRemappingKeyofResult2.ts, 15, 23)) | ||
>TActors : Symbol(TActors, Decl(keyRemappingKeyofResult2.ts, 15, 23)) | ||
>K : Symbol(K, Decl(keyRemappingKeyofResult2.ts, 17, 5)) | ||
|
||
}; | ||
}): { | ||
createMachine: ( | ||
>createMachine : Symbol(createMachine, Decl(keyRemappingKeyofResult2.ts, 19, 5)) | ||
|
||
config: StateMachineConfig< | ||
>config : Symbol(config, Decl(keyRemappingKeyofResult2.ts, 20, 18)) | ||
>StateMachineConfig : Symbol(StateMachineConfig, Decl(keyRemappingKeyofResult2.ts, 7, 2)) | ||
|
||
Values<{ | ||
>Values : Symbol(Values, Decl(keyRemappingKeyofResult2.ts, 0, 0)) | ||
|
||
[K in keyof TActors as K & string]: { | ||
>K : Symbol(K, Decl(keyRemappingKeyofResult2.ts, 23, 9)) | ||
>TActors : Symbol(TActors, Decl(keyRemappingKeyofResult2.ts, 15, 23)) | ||
>K : Symbol(K, Decl(keyRemappingKeyofResult2.ts, 23, 9)) | ||
|
||
src: K; | ||
>src : Symbol(src, Decl(keyRemappingKeyofResult2.ts, 23, 45)) | ||
>K : Symbol(K, Decl(keyRemappingKeyofResult2.ts, 23, 9)) | ||
|
||
logic: TActors[K]; | ||
>logic : Symbol(logic, Decl(keyRemappingKeyofResult2.ts, 24, 17)) | ||
>TActors : Symbol(TActors, Decl(keyRemappingKeyofResult2.ts, 15, 23)) | ||
>K : Symbol(K, Decl(keyRemappingKeyofResult2.ts, 23, 9)) | ||
|
||
}; | ||
}> | ||
>, | ||
) => void; | ||
}; | ||
|
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,59 @@ | ||
//// [tests/cases/compiler/keyRemappingKeyofResult2.ts] //// | ||
|
||
=== keyRemappingKeyofResult2.ts === | ||
// https://github.com/microsoft/TypeScript/issues/56239 | ||
|
||
type Values<T> = T[keyof T]; | ||
>Values : Values<T> | ||
|
||
type ProvidedActor = { | ||
>ProvidedActor : { src: string; logic: unknown; } | ||
|
||
src: string; | ||
>src : string | ||
|
||
logic: unknown; | ||
>logic : unknown | ||
|
||
}; | ||
|
||
interface StateMachineConfig<TActors extends ProvidedActor> { | ||
invoke: { | ||
>invoke : { src: TActors["src"]; } | ||
|
||
src: TActors["src"]; | ||
>src : TActors["src"] | ||
|
||
}; | ||
} | ||
|
||
declare function setup<TActors extends Record<string, unknown>>(_: { | ||
>setup : <TActors extends Record<string, unknown>>(_: { actors: { [K in keyof TActors]: TActors[K]; }; }) => { createMachine: (config: StateMachineConfig<Values<{ [K in keyof TActors as K & string]: { src: K; logic: TActors[K]; }; }>>) => void; } | ||
>_ : { actors: { [K in keyof TActors]: TActors[K]; }; } | ||
|
||
actors: { | ||
>actors : { [K in keyof TActors]: TActors[K]; } | ||
|
||
[K in keyof TActors]: TActors[K]; | ||
}; | ||
}): { | ||
createMachine: ( | ||
>createMachine : (config: StateMachineConfig<Values<{ [K in keyof TActors as K & string]: { src: K; logic: TActors[K]; }; }>>) => void | ||
|
||
config: StateMachineConfig< | ||
>config : StateMachineConfig<Values<{ [K in keyof TActors as K & string]: { src: K; logic: TActors[K]; }; }>> | ||
|
||
Values<{ | ||
[K in keyof TActors as K & string]: { | ||
src: K; | ||
>src : K | ||
|
||
logic: TActors[K]; | ||
>logic : TActors[K] | ||
|
||
}; | ||
}> | ||
>, | ||
) => void; | ||
}; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
// https://github.com/microsoft/TypeScript/issues/56239 | ||
|
||
type Values<T> = T[keyof T]; | ||
|
||
type ProvidedActor = { | ||
src: string; | ||
logic: unknown; | ||
}; | ||
|
||
interface StateMachineConfig<TActors extends ProvidedActor> { | ||
invoke: { | ||
src: TActors["src"]; | ||
}; | ||
} | ||
|
||
declare function setup<TActors extends Record<string, unknown>>(_: { | ||
actors: { | ||
[K in keyof TActors]: TActors[K]; | ||
}; | ||
}): { | ||
createMachine: ( | ||
config: StateMachineConfig< | ||
Values<{ | ||
[K in keyof TActors as K & string]: { | ||
src: K; | ||
logic: TActors[K]; | ||
}; | ||
}> | ||
>, | ||
) => void; | ||
}; |
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.
UnionReduction.None
was added here to keep this assignment allowed.I think that without it the union reduction was forgetting about
'str'
since it's a subtype ofstring
.string
is a valid inputK
but it is meant to be filtered away byDistributiveNonIndex
. So with the union reduction on, I have ended up removing'str'
entirely - breaking the test case.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'm somewhat unsure - but maybe the union reduction could still mess this up when another union gets created manually with that as one of its members? Maybe I should explore a different fix that would do the same~ instantiation but later on, when comparing some constraints?
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.
UnionReduction.None
is, in some ways, a hack to make contextual typing work a little bit better (and avoid the perf overhead of subtype reduction we don't need to bother doing). The union members could, very easily, evaporate later on down the line.IMO, it'd be better to leave this case as producing a generic
keyof
type (especially since thisnameType
could still just be filtering a homomorphic mapped type), and improve constraint-following logic inside the generic-keyof
-over-mapped-type case instructuredTypeRelatedToWorker
- specifically, we already have a generic-index-type-target case that does the right thing (tm) - that probably just needs to be brought up and situationally applied when the source is a generic index, too. Since our only special case forkeyof
on the source side right now isit could probably use some more specifics, since it's definitely true that generic
keyof
s (over mapped types) can have constraints more specific thanstring | number | symbol
.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.
Thank you for the advice! I now reused the mentioned logic for
keyof GenericMappedType
as the target type so now the whole fix is withinstructuredTypeRelatedToWorker
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.
Hah, I never linked it here, but after I looked over this, I was assigned another issue that tracked back to the same thing, so I ended up writing #56742, which basically scooped this change. Do you want to sync this and at least add the new test cases? In #56742 I used much more specific carve-outs of which
keyof
types this applies to than the very general check here (and actually use a slightly different source key type).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.
#56742 didn't fix #56239 , see the nightly playground here. I'll investigate what's the difference between our approaches in the coming days and report back.
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 reason this wasn't fixed is that
keyof { [K in keyof TActors as K & string]: { src: K; logic: TActors[K]; }; }
wasn't treated as a deferred index type (although I'd argue that it clearly is deferred). So the logic added by #56742 couldn't kick in.I recently changed that in #60528 and I think that essentially supersedes this PR so I just merged it in there now to keep the tests.