-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Infer type parameters from indexes on those parameters #53017
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
39
commits into
microsoft:main
Choose a base branch
from
Andarist:infer-concrete-properties-of-reverse-mapped-types
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.
+4,770
−143
Open
Changes from 14 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
3451575
Infer type parameters from indexes on those parameters
weswigham f5b208c
Greatly simplify partial inference type instantiation through use of …
weswigham ba064ac
Add many more tests showing current behaviors
weswigham 93afc10
Discriminate partial inferences if not complete enough to satisfy con…
weswigham 49e1961
Move case to prefered location
weswigham 96772e5
Small refactor to reduce how many comparisons are performed
weswigham 2e0c635
Infer reverse mapped types based on inferences made for its concrete …
Andarist 7b8cf10
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist e7e4e70
Merge remote-tracking branch 'weswigham/index-combined-inferences' in…
Andarist d0e8b8b
update baselines, note: some are broken
Andarist e5d0ea8
use new `inference.indexes` in reverse mapped type inference
Andarist b0b2fcb
update reverse mapped baseline
Andarist 7fe118a
Call `getActualTypeVariable` appropriately to fix inference for a tup…
Andarist 50e6a0c
Avoid inferring an index under `InferencePriority.NakedTypeVariable`
Andarist 4febb9c
always discard aggregate inference that is not assignable to the cons…
Andarist c2d4b0f
bring back the union discriminating logic
Andarist 11b02d0
fixed incorrect indexed access in tests
Andarist 97adac5
only use the aggregate inference when there are no other candidates
Andarist 8ffcb94
do not collect index-based inferences from non-inferrable types
Andarist 192d8b7
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist 5fa585c
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist 375c127
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist a62749c
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist 2f86371
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist 9825bcd
update baselines
Andarist 46e280b
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist c90fb20
instantiate aggregate inference
Andarist 2b580c8
experiment with `eraseSelfMapper`
Andarist a390703
experiment further
Andarist 15eda09
drop unused code
Andarist e3f6b43
discard never aggregate inferences
Andarist 1911406
prefer default
Andarist 20d6feb
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist a554bbf
change condition for indexes gathering
Andarist 22ddecc
simplify test
Andarist ced3cc1
refactor
Andarist a09f410
remove `PartialInference`
Andarist 7bc77f1
add tests
Andarist 097b8e6
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
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
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
130 changes: 130 additions & 0 deletions
130
tests/baselines/reference/indexAccessCombinedInference.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,130 @@ | ||
| tests/cases/compiler/indexAccessCombinedInference.ts(104,21): error TS2536: Type '"0"' cannot be used to index type 'this'. | ||
| tests/cases/compiler/indexAccessCombinedInference.ts(106,49): error TS2536: Type '"0"' cannot be used to index type 'T'. | ||
| tests/cases/compiler/indexAccessCombinedInference.ts(106,60): error TS2536: Type '"0"' cannot be used to index type 'T'. | ||
| tests/cases/compiler/indexAccessCombinedInference.ts(106,77): error TS2536: Type '"1"' cannot be used to index type 'T'. | ||
| tests/cases/compiler/indexAccessCombinedInference.ts(106,86): error TS2536: Type '"0"' cannot be used to index type 'T'. | ||
| tests/cases/compiler/indexAccessCombinedInference.ts(106,110): error TS2536: Type '"1"' cannot be used to index type 'T'. | ||
|
|
||
|
|
||
| ==== tests/cases/compiler/indexAccessCombinedInference.ts (6 errors) ==== | ||
| // Simple case | ||
| interface Args { | ||
| TA: object, | ||
| TY: object | ||
| } | ||
|
|
||
| declare function foo<T extends Args>( | ||
| a: T["TA"], | ||
| b: T["TY"]): T["TA"] & T["TY"]; | ||
|
|
||
| const x = foo({ | ||
| x: { | ||
| j: 12, | ||
| i: 11 | ||
| } | ||
| }, { y: 42 }); | ||
|
|
||
| // Union result type | ||
| interface A { | ||
| foo: number; | ||
| } | ||
| interface B { | ||
| bar: string; | ||
| } | ||
| declare const something: A | B; | ||
|
|
||
| const y = foo(something, { bat: 42 }); | ||
|
|
||
| // Union key type | ||
| interface Args2 { | ||
| TA?: object, // Optional since only one of TA or TB needs to be infered in the below argument list | ||
| TB?: object, | ||
| TY: object | ||
| } | ||
| declare function foo2<T extends Args2>( | ||
| a: T["TA"] | T["TB"], | ||
| b: T["TY"]): {a: T["TA"], b: T["TB"]} & T["TY"]; | ||
| declare function foo3<T extends Args2>( // Morally equivalent to foo2 | ||
| a: T["TA" | "TB"], | ||
| b: T["TY"]): {a: T["TA"], b: T["TB"]} & T["TY"]; | ||
| let z = foo2({ | ||
| x: { | ||
| j: 12, | ||
| i: 11 | ||
| } | ||
| }, { y: 42 }); | ||
| let zz = foo3({ | ||
| x: { | ||
| j: 12, | ||
| i: 11 | ||
| } | ||
| }, { y: 42 }); | ||
| z = zz; | ||
| zz = z; | ||
|
|
||
| // Higher-order | ||
| interface Args3 { | ||
| Key: "A" | "B", | ||
| A: object, | ||
| B: object, | ||
| Merge: object, | ||
| } | ||
| declare const either: "A" | "B"; | ||
| declare function pickOne<T extends Args3>(key: T["Key"], left: T["A"], right: T["B"], into: T["Merge"]): T[T["Key"]] & T["Merge"]; | ||
|
|
||
| const opt1 = pickOne("A", {x: 12}, {y: ""}, {z: /./}); | ||
| const opt2 = pickOne("B", {x: 12}, {y: ""}, {z: /./}); | ||
| const opt3 = pickOne(either, {x: 12}, {y: ""}, {z: /./}); | ||
|
|
||
| const pickDelayed = <TKey extends Args3["Key"]>(x: TKey) => pickOne(x, {j: x}, {i: x}, {chosen: x}); | ||
| const opt4 = pickDelayed("A"); | ||
| const opt5 = pickDelayed("B"); | ||
| const opt6 = pickDelayed(either); | ||
|
|
||
| // Reopenable | ||
| interface Args3 { | ||
| /** | ||
| * One must make patched parameters optional, otherwise signatures expecting the unpatched | ||
| * interface (ie, pickOne above) will not be able to produce a type satisfying the interface | ||
| * (as there are no inference sites for the new members) and will fall back to the constraints on each member | ||
| */ | ||
| Extra?: object, | ||
| } | ||
| declare function pickOne<T extends Args3>(key: T["Key"], left: T["A"], right: T["B"], into: T["Merge"], extra: T["Extra"]): T[T["Key"]] & {into: T["Merge"], extra: T["Extra"]}; | ||
| const opt7 = pickOne("A", {x: 12}, {y: ""}, {z: /./}, {z: /./}); | ||
| const opt8 = pickOne("B", {x: 12}, {y: ""}, {z: /./}, {z: /./}); | ||
| const opt9 = pickOne(either, {x: 12}, {y: ""}, {z: /./}, {z: /./}); | ||
|
|
||
| // Interactions with `this` types | ||
| interface TPicker { | ||
| Key: keyof this, | ||
| X: number, | ||
| Y: string | ||
| } | ||
| declare function chooseLiteral<T extends TPicker>(choice: T["Key"], x: T["X"], y:T["Y"]): T[T["Key"]]; | ||
| const cx = chooseLiteral("X", 1, "no"); | ||
| const cy = chooseLiteral("Y", 0, "yes"); | ||
| const ceither = chooseLiteral("X" as "X" | "Y", 1, "yes"); | ||
| const cneither = chooseLiteral("Key", 0, "no"); | ||
|
|
||
| // Multiple inference sites | ||
| interface Args4 { | ||
| 0: object, | ||
| 1: Record<keyof this["0"], Function>, | ||
| ~~~~~~~~~ | ||
| !!! error TS2536: Type '"0"' cannot be used to index type 'this'. | ||
| } | ||
| declare function dualInputs<T extends Args4>(x: T["0"], y: T["0"], toDelay: T["1"]): T["0"] & {transformers: T["1"]}; | ||
| ~~~~~~ | ||
| !!! error TS2536: Type '"0"' cannot be used to index type 'T'. | ||
| ~~~~~~ | ||
| !!! error TS2536: Type '"0"' cannot be used to index type 'T'. | ||
| ~~~~~~ | ||
| !!! error TS2536: Type '"1"' cannot be used to index type 'T'. | ||
| ~~~~~~ | ||
| !!! error TS2536: Type '"0"' cannot be used to index type 'T'. | ||
| ~~~~~~ | ||
| !!! error TS2536: Type '"1"' cannot be used to index type 'T'. | ||
|
|
||
| const result = dualInputs({x: 0}, {x: 1}, {x: () => ""}); | ||
|
|
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.