-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Default reverse mapped type inference to its constraint #56300
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
10
commits into
microsoft:main
Choose a base branch
from
Andarist:reverse-mapped-type-default-to-constraint
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 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
720a860
Default reverse mapped type inference to its constraint
Andarist b3cd45e
Merge remote-tracking branch 'origin/main' into reverse-mapped-type-d…
Andarist a0d8bcc
improve per-property constraints by using other available inferences
Andarist b5f5aab
Merge remote-tracking branch 'origin/main' into reverse-mapped-type-d…
Andarist 50f63e4
change strategy to reverse mapped type clones
Andarist 75408c7
fixed `propertyNameType` passed down in case of tuples
Andarist 93c2643
simplify and cache
Andarist 471ad1f
fmt
Andarist 620e38b
Merge remote-tracking branch 'origin/main' into reverse-mapped-type-d…
Andarist a4705ef
add silly test
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
86 changes: 86 additions & 0 deletions
86
tests/baselines/reference/reverseMappedDefaultInferenceToConstraint.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,86 @@ | ||
| //// [tests/cases/compiler/reverseMappedDefaultInferenceToConstraint.ts] //// | ||
|
|
||
| === reverseMappedDefaultInferenceToConstraint.ts === | ||
| // https://github.com/microsoft/TypeScript/issues/56241 | ||
|
|
||
| interface ParameterizedObject { | ||
| >ParameterizedObject : Symbol(ParameterizedObject, Decl(reverseMappedDefaultInferenceToConstraint.ts, 0, 0)) | ||
|
|
||
| type: string; | ||
| >type : Symbol(ParameterizedObject.type, Decl(reverseMappedDefaultInferenceToConstraint.ts, 2, 31)) | ||
|
|
||
| params?: Record<string, unknown>; | ||
| >params : Symbol(ParameterizedObject.params, Decl(reverseMappedDefaultInferenceToConstraint.ts, 3, 15)) | ||
| >Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
| } | ||
|
|
||
| declare function setup< | ||
| >setup : Symbol(setup, Decl(reverseMappedDefaultInferenceToConstraint.ts, 5, 1)) | ||
|
|
||
| TContext, | ||
| >TContext : Symbol(TContext, Decl(reverseMappedDefaultInferenceToConstraint.ts, 7, 23)) | ||
|
|
||
| TGuards extends Record<string, ParameterizedObject["params"] | undefined>, | ||
| >TGuards : Symbol(TGuards, Decl(reverseMappedDefaultInferenceToConstraint.ts, 8, 11)) | ||
| >Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
| >ParameterizedObject : Symbol(ParameterizedObject, Decl(reverseMappedDefaultInferenceToConstraint.ts, 0, 0)) | ||
|
|
||
| >(_: { | ||
| >_ : Symbol(_, Decl(reverseMappedDefaultInferenceToConstraint.ts, 10, 2)) | ||
|
|
||
| types: { | ||
| >types : Symbol(types, Decl(reverseMappedDefaultInferenceToConstraint.ts, 10, 6)) | ||
|
|
||
| context: TContext; | ||
| >context : Symbol(context, Decl(reverseMappedDefaultInferenceToConstraint.ts, 11, 10)) | ||
| >TContext : Symbol(TContext, Decl(reverseMappedDefaultInferenceToConstraint.ts, 7, 23)) | ||
|
|
||
| }; | ||
| guards: { | ||
| >guards : Symbol(guards, Decl(reverseMappedDefaultInferenceToConstraint.ts, 13, 4)) | ||
|
|
||
| [K in keyof TGuards]: (context: TContext, params: TGuards[K]) => void; | ||
| >K : Symbol(K, Decl(reverseMappedDefaultInferenceToConstraint.ts, 15, 5)) | ||
| >TGuards : Symbol(TGuards, Decl(reverseMappedDefaultInferenceToConstraint.ts, 8, 11)) | ||
| >context : Symbol(context, Decl(reverseMappedDefaultInferenceToConstraint.ts, 15, 27)) | ||
| >TContext : Symbol(TContext, Decl(reverseMappedDefaultInferenceToConstraint.ts, 7, 23)) | ||
| >params : Symbol(params, Decl(reverseMappedDefaultInferenceToConstraint.ts, 15, 45)) | ||
| >TGuards : Symbol(TGuards, Decl(reverseMappedDefaultInferenceToConstraint.ts, 8, 11)) | ||
| >K : Symbol(K, Decl(reverseMappedDefaultInferenceToConstraint.ts, 15, 5)) | ||
|
|
||
| }; | ||
| }): TGuards; | ||
| >TGuards : Symbol(TGuards, Decl(reverseMappedDefaultInferenceToConstraint.ts, 8, 11)) | ||
|
|
||
| const result = setup({ | ||
| >result : Symbol(result, Decl(reverseMappedDefaultInferenceToConstraint.ts, 19, 5)) | ||
| >setup : Symbol(setup, Decl(reverseMappedDefaultInferenceToConstraint.ts, 5, 1)) | ||
|
|
||
| types: { | ||
| >types : Symbol(types, Decl(reverseMappedDefaultInferenceToConstraint.ts, 19, 22)) | ||
|
|
||
| context: { | ||
| >context : Symbol(context, Decl(reverseMappedDefaultInferenceToConstraint.ts, 20, 10)) | ||
|
|
||
| count: 100, | ||
| >count : Symbol(count, Decl(reverseMappedDefaultInferenceToConstraint.ts, 21, 14)) | ||
|
|
||
| }, | ||
| }, | ||
| guards: { | ||
| >guards : Symbol(guards, Decl(reverseMappedDefaultInferenceToConstraint.ts, 24, 4)) | ||
|
|
||
| checkFoo: (_, { foo }: { foo: string }) => foo === "foo", | ||
| >checkFoo : Symbol(checkFoo, Decl(reverseMappedDefaultInferenceToConstraint.ts, 25, 11)) | ||
| >_ : Symbol(_, Decl(reverseMappedDefaultInferenceToConstraint.ts, 26, 15)) | ||
| >foo : Symbol(foo, Decl(reverseMappedDefaultInferenceToConstraint.ts, 26, 19)) | ||
| >foo : Symbol(foo, Decl(reverseMappedDefaultInferenceToConstraint.ts, 26, 28)) | ||
| >foo : Symbol(foo, Decl(reverseMappedDefaultInferenceToConstraint.ts, 26, 19)) | ||
|
|
||
| alwaysTrue: (_) => true, | ||
| >alwaysTrue : Symbol(alwaysTrue, Decl(reverseMappedDefaultInferenceToConstraint.ts, 26, 61)) | ||
| >_ : Symbol(_, Decl(reverseMappedDefaultInferenceToConstraint.ts, 27, 17)) | ||
|
|
||
| }, | ||
| }); | ||
|
|
81 changes: 81 additions & 0 deletions
81
tests/baselines/reference/reverseMappedDefaultInferenceToConstraint.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,81 @@ | ||
| //// [tests/cases/compiler/reverseMappedDefaultInferenceToConstraint.ts] //// | ||
|
|
||
| === reverseMappedDefaultInferenceToConstraint.ts === | ||
| // https://github.com/microsoft/TypeScript/issues/56241 | ||
|
|
||
| interface ParameterizedObject { | ||
| type: string; | ||
| >type : string | ||
|
|
||
| params?: Record<string, unknown>; | ||
| >params : Record<string, unknown> | undefined | ||
| } | ||
|
|
||
| declare function setup< | ||
| >setup : <TContext, TGuards extends Record<string, Record<string, unknown> | undefined>>(_: { types: { context: TContext;}; guards: { [K in keyof TGuards]: (context: TContext, params: TGuards[K]) => void; }; }) => TGuards | ||
|
|
||
| TContext, | ||
| TGuards extends Record<string, ParameterizedObject["params"] | undefined>, | ||
| >(_: { | ||
| >_ : { types: { context: TContext;}; guards: { [K in keyof TGuards]: (context: TContext, params: TGuards[K]) => void; }; } | ||
|
|
||
| types: { | ||
| >types : { context: TContext; } | ||
|
|
||
| context: TContext; | ||
| >context : TContext | ||
|
|
||
| }; | ||
| guards: { | ||
| >guards : { [K in keyof TGuards]: (context: TContext, params: TGuards[K]) => void; } | ||
|
|
||
| [K in keyof TGuards]: (context: TContext, params: TGuards[K]) => void; | ||
| >context : TContext | ||
| >params : TGuards[K] | ||
|
|
||
| }; | ||
| }): TGuards; | ||
|
|
||
| const result = setup({ | ||
| >result : { checkFoo: { foo: string; }; alwaysTrue: Record<string, unknown> | undefined; } | ||
| >setup({ types: { context: { count: 100, }, }, guards: { checkFoo: (_, { foo }: { foo: string }) => foo === "foo", alwaysTrue: (_) => true, },}) : { checkFoo: { foo: string; }; alwaysTrue: Record<string, unknown> | undefined; } | ||
| >setup : <TContext, TGuards extends Record<string, Record<string, unknown> | undefined>>(_: { types: { context: TContext; }; guards: { [K in keyof TGuards]: (context: TContext, params: TGuards[K]) => void; }; }) => TGuards | ||
| >{ types: { context: { count: 100, }, }, guards: { checkFoo: (_, { foo }: { foo: string }) => foo === "foo", alwaysTrue: (_) => true, },} : { types: { context: { count: number; }; }; guards: { checkFoo: (_: { count: number; }, { foo }: { foo: string; }) => boolean; alwaysTrue: (_: { count: number; }) => boolean; }; } | ||
|
|
||
| types: { | ||
| >types : { context: { count: number; }; } | ||
| >{ context: { count: 100, }, } : { context: { count: number; }; } | ||
|
|
||
| context: { | ||
| >context : { count: number; } | ||
| >{ count: 100, } : { count: number; } | ||
|
|
||
| count: 100, | ||
| >count : number | ||
| >100 : 100 | ||
|
|
||
| }, | ||
| }, | ||
| guards: { | ||
| >guards : { checkFoo: (_: { count: number; }, { foo }: { foo: string; }) => boolean; alwaysTrue: (_: { count: number; }) => boolean; } | ||
| >{ checkFoo: (_, { foo }: { foo: string }) => foo === "foo", alwaysTrue: (_) => true, } : { checkFoo: (_: { count: number; }, { foo }: { foo: string; }) => boolean; alwaysTrue: (_: { count: number; }) => boolean; } | ||
|
|
||
| checkFoo: (_, { foo }: { foo: string }) => foo === "foo", | ||
| >checkFoo : (_: { count: number; }, { foo }: { foo: string; }) => boolean | ||
| >(_, { foo }: { foo: string }) => foo === "foo" : (_: { count: number; }, { foo }: { foo: string; }) => boolean | ||
| >_ : { count: number; } | ||
| >foo : string | ||
| >foo : string | ||
| >foo === "foo" : boolean | ||
| >foo : string | ||
| >"foo" : "foo" | ||
|
|
||
| alwaysTrue: (_) => true, | ||
| >alwaysTrue : (_: { count: number; }) => boolean | ||
| >(_) => true : (_: { count: number; }) => boolean | ||
| >_ : { count: number; } | ||
| >true : true | ||
|
|
||
| }, | ||
| }); | ||
|
|
33 changes: 33 additions & 0 deletions
33
tests/cases/compiler/reverseMappedDefaultInferenceToConstraint.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,33 @@ | ||
| // @strict: true | ||
| // @noEmit: true | ||
|
|
||
| // https://github.com/microsoft/TypeScript/issues/56241 | ||
|
|
||
| interface ParameterizedObject { | ||
| type: string; | ||
| params?: Record<string, unknown>; | ||
| } | ||
|
|
||
| declare function setup< | ||
| TContext, | ||
| TGuards extends Record<string, ParameterizedObject["params"] | undefined>, | ||
| >(_: { | ||
| types: { | ||
| context: TContext; | ||
| }; | ||
| guards: { | ||
| [K in keyof TGuards]: (context: TContext, params: TGuards[K]) => void; | ||
| }; | ||
| }): TGuards; | ||
|
|
||
| const result = setup({ | ||
| types: { | ||
| context: { | ||
| count: 100, | ||
| }, | ||
| }, | ||
| guards: { | ||
| checkFoo: (_, { foo }: { foo: string }) => foo === "foo", | ||
| alwaysTrue: (_) => true, | ||
| }, | ||
| }); |
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.
getBaseConstraintOfTyperecursively walks all constraints - so ifT extends UandU extends VandV extends number, you getnumberout. Which means you skip inferences to those intermediate results. I think you'd want to usegetConstraintOfType, right? Because, in this case, if you haveT[K] extends UandU extends number,Uis a better (and more specific) answer thannumber.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.
Sort of. It's true it won't immediately walk all constraints but at the same time, it's not inference-aware anyway. If I call it then I end up with
T[keyof T]and when relating the inferred type to the instantiated constraint (from withingetInferredType) we eventually simplify this toUand to its base constraint. So to some extent, this leads to the same thing and the same problems. To incorporate inferences of other type parameters I'd have to utilizecontext.nonFixingMapper.It's not immediately obvious how to get access to this from within this function (
inferReverseMappedType) but that certainly can be done. A bigger problem though is thatinferReverseMappedTypegets called at different times for object and array/tuple types - when dealing with the latter it's called eagerly~. So it gets called right from withininferFromTypes. This introduces differences in data availability between the object and the array/tuple case.For instance, let's take a look at this:
Since
inferReverseMappedTypegets called eagerly here to create the reverse mapped tuple type it's not possible to observe theU's inferred type. That is different from the equivalent object variant, like:This isn't exactly a new problem. I've encountered this already in the past at least once or twice.
I'd really like to solve this. It feels like a separate issue though - but then: what would be the acceptable state of this PR to get this one in?
inferReverseMappedTypeaware of the inference context (to get access to its.nonFixingMapper) be enough?