-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add instantiation rules for reverse mapped types #42449
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
Merged
weswigham
merged 4 commits into
microsoft:master
from
weswigham:reverse-mapped-instantiation
Apr 27, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1cdbfee
Add instantiation rules for reverse mapped types
weswigham 81c9f3e
Merge branch 'master' into reverse-mapped-instantiation
weswigham eec6c46
Add smaller example of same issue
weswigham 14bdc30
Merge branch 'master' into reverse-mapped-instantiation
weswigham 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
53 changes: 53 additions & 0 deletions
53
tests/baselines/reference/genericFunctionsAndConditionalInference.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,53 @@ | ||
//// [genericFunctionsAndConditionalInference.ts] | ||
type Boxified<T> = { [P in keyof T]: { value: T[P]} }; | ||
|
||
declare function unboxify<T>(obj: Boxified<T>): T; | ||
|
||
function foo<U, V>(obj: { u: { value: U }, v: { value: V } }) { | ||
return unboxify(obj); | ||
} | ||
|
||
let qq = foo({ u: { value: 10 }, v: { value: 'hello'} }); // { u: U, v: V } but should be { u: number, v: string } | ||
|
||
// From #42385 | ||
interface Targets<A> { | ||
left: A | ||
right: A | ||
} | ||
type Target = keyof Targets<any> | ||
type Result<F extends Target, A> = Targets<A>[F] | ||
|
||
type LR<F extends Target, L, R> = [F] extends ["left"] ? L : R | ||
|
||
interface Ops<F extends Target> { | ||
_f: F | ||
str: Result<F, string> | ||
num: Result<F, number> | ||
lr<I, O>(a: Result<F, I>, o: Result<F, O>): Result<F, LR<F, I, O>> | ||
dict: <P>(p: {[k in keyof P]: Result<F, P[k]>}) => Result<F, P> | ||
} | ||
const left: Ops<"left"> = {} as any | ||
const right: Ops<"right"> = {} as any | ||
|
||
const ok = <F extends Target>(at: Ops<F>) => ({lr: at.lr(at.str, at.num)}) | ||
const orphaned = <F extends Target>(at: Ops<F>) => at.dict(ok(at)) | ||
|
||
const leftOk = ok(left) | ||
const leftOrphaned = orphaned(left) | ||
|
||
const rightOk = ok(right) | ||
const rightOrphaned = orphaned(right) | ||
|
||
//// [genericFunctionsAndConditionalInference.js] | ||
function foo(obj) { | ||
return unboxify(obj); | ||
} | ||
var qq = foo({ u: { value: 10 }, v: { value: 'hello' } }); // { u: U, v: V } but should be { u: number, v: string } | ||
var left = {}; | ||
var right = {}; | ||
var ok = function (at) { return ({ lr: at.lr(at.str, at.num) }); }; | ||
var orphaned = function (at) { return at.dict(ok(at)); }; | ||
var leftOk = ok(left); | ||
var leftOrphaned = orphaned(left); | ||
var rightOk = ok(right); | ||
var rightOrphaned = orphaned(right); |
190 changes: 190 additions & 0 deletions
190
tests/baselines/reference/genericFunctionsAndConditionalInference.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,190 @@ | ||
=== tests/cases/compiler/genericFunctionsAndConditionalInference.ts === | ||
type Boxified<T> = { [P in keyof T]: { value: T[P]} }; | ||
>Boxified : Symbol(Boxified, Decl(genericFunctionsAndConditionalInference.ts, 0, 0)) | ||
>T : Symbol(T, Decl(genericFunctionsAndConditionalInference.ts, 0, 14)) | ||
>P : Symbol(P, Decl(genericFunctionsAndConditionalInference.ts, 0, 22)) | ||
>T : Symbol(T, Decl(genericFunctionsAndConditionalInference.ts, 0, 14)) | ||
>value : Symbol(value, Decl(genericFunctionsAndConditionalInference.ts, 0, 38)) | ||
>T : Symbol(T, Decl(genericFunctionsAndConditionalInference.ts, 0, 14)) | ||
>P : Symbol(P, Decl(genericFunctionsAndConditionalInference.ts, 0, 22)) | ||
|
||
declare function unboxify<T>(obj: Boxified<T>): T; | ||
>unboxify : Symbol(unboxify, Decl(genericFunctionsAndConditionalInference.ts, 0, 54)) | ||
>T : Symbol(T, Decl(genericFunctionsAndConditionalInference.ts, 2, 26)) | ||
>obj : Symbol(obj, Decl(genericFunctionsAndConditionalInference.ts, 2, 29)) | ||
>Boxified : Symbol(Boxified, Decl(genericFunctionsAndConditionalInference.ts, 0, 0)) | ||
>T : Symbol(T, Decl(genericFunctionsAndConditionalInference.ts, 2, 26)) | ||
>T : Symbol(T, Decl(genericFunctionsAndConditionalInference.ts, 2, 26)) | ||
|
||
function foo<U, V>(obj: { u: { value: U }, v: { value: V } }) { | ||
>foo : Symbol(foo, Decl(genericFunctionsAndConditionalInference.ts, 2, 50)) | ||
>U : Symbol(U, Decl(genericFunctionsAndConditionalInference.ts, 4, 13)) | ||
>V : Symbol(V, Decl(genericFunctionsAndConditionalInference.ts, 4, 15)) | ||
>obj : Symbol(obj, Decl(genericFunctionsAndConditionalInference.ts, 4, 19)) | ||
>u : Symbol(u, Decl(genericFunctionsAndConditionalInference.ts, 4, 25)) | ||
>value : Symbol(value, Decl(genericFunctionsAndConditionalInference.ts, 4, 30)) | ||
>U : Symbol(U, Decl(genericFunctionsAndConditionalInference.ts, 4, 13)) | ||
>v : Symbol(v, Decl(genericFunctionsAndConditionalInference.ts, 4, 42)) | ||
>value : Symbol(value, Decl(genericFunctionsAndConditionalInference.ts, 4, 47)) | ||
>V : Symbol(V, Decl(genericFunctionsAndConditionalInference.ts, 4, 15)) | ||
|
||
return unboxify(obj); | ||
>unboxify : Symbol(unboxify, Decl(genericFunctionsAndConditionalInference.ts, 0, 54)) | ||
>obj : Symbol(obj, Decl(genericFunctionsAndConditionalInference.ts, 4, 19)) | ||
} | ||
|
||
let qq = foo({ u: { value: 10 }, v: { value: 'hello'} }); // { u: U, v: V } but should be { u: number, v: string } | ||
>qq : Symbol(qq, Decl(genericFunctionsAndConditionalInference.ts, 8, 3)) | ||
>foo : Symbol(foo, Decl(genericFunctionsAndConditionalInference.ts, 2, 50)) | ||
>u : Symbol(u, Decl(genericFunctionsAndConditionalInference.ts, 8, 14)) | ||
>value : Symbol(value, Decl(genericFunctionsAndConditionalInference.ts, 8, 19)) | ||
>v : Symbol(v, Decl(genericFunctionsAndConditionalInference.ts, 8, 32)) | ||
>value : Symbol(value, Decl(genericFunctionsAndConditionalInference.ts, 8, 37)) | ||
|
||
// From #42385 | ||
interface Targets<A> { | ||
>Targets : Symbol(Targets, Decl(genericFunctionsAndConditionalInference.ts, 8, 57)) | ||
>A : Symbol(A, Decl(genericFunctionsAndConditionalInference.ts, 11, 18)) | ||
|
||
left: A | ||
>left : Symbol(Targets.left, Decl(genericFunctionsAndConditionalInference.ts, 11, 22)) | ||
>A : Symbol(A, Decl(genericFunctionsAndConditionalInference.ts, 11, 18)) | ||
|
||
right: A | ||
>right : Symbol(Targets.right, Decl(genericFunctionsAndConditionalInference.ts, 12, 11)) | ||
>A : Symbol(A, Decl(genericFunctionsAndConditionalInference.ts, 11, 18)) | ||
} | ||
type Target = keyof Targets<any> | ||
>Target : Symbol(Target, Decl(genericFunctionsAndConditionalInference.ts, 14, 1)) | ||
>Targets : Symbol(Targets, Decl(genericFunctionsAndConditionalInference.ts, 8, 57)) | ||
|
||
type Result<F extends Target, A> = Targets<A>[F] | ||
>Result : Symbol(Result, Decl(genericFunctionsAndConditionalInference.ts, 15, 32)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 16, 12)) | ||
>Target : Symbol(Target, Decl(genericFunctionsAndConditionalInference.ts, 14, 1)) | ||
>A : Symbol(A, Decl(genericFunctionsAndConditionalInference.ts, 16, 29)) | ||
>Targets : Symbol(Targets, Decl(genericFunctionsAndConditionalInference.ts, 8, 57)) | ||
>A : Symbol(A, Decl(genericFunctionsAndConditionalInference.ts, 16, 29)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 16, 12)) | ||
|
||
type LR<F extends Target, L, R> = [F] extends ["left"] ? L : R | ||
>LR : Symbol(LR, Decl(genericFunctionsAndConditionalInference.ts, 16, 48)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 18, 8)) | ||
>Target : Symbol(Target, Decl(genericFunctionsAndConditionalInference.ts, 14, 1)) | ||
>L : Symbol(L, Decl(genericFunctionsAndConditionalInference.ts, 18, 25)) | ||
>R : Symbol(R, Decl(genericFunctionsAndConditionalInference.ts, 18, 28)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 18, 8)) | ||
>L : Symbol(L, Decl(genericFunctionsAndConditionalInference.ts, 18, 25)) | ||
>R : Symbol(R, Decl(genericFunctionsAndConditionalInference.ts, 18, 28)) | ||
|
||
interface Ops<F extends Target> { | ||
>Ops : Symbol(Ops, Decl(genericFunctionsAndConditionalInference.ts, 18, 62)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 20, 14)) | ||
>Target : Symbol(Target, Decl(genericFunctionsAndConditionalInference.ts, 14, 1)) | ||
|
||
_f: F | ||
>_f : Symbol(Ops._f, Decl(genericFunctionsAndConditionalInference.ts, 20, 33)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 20, 14)) | ||
|
||
str: Result<F, string> | ||
>str : Symbol(Ops.str, Decl(genericFunctionsAndConditionalInference.ts, 21, 9)) | ||
>Result : Symbol(Result, Decl(genericFunctionsAndConditionalInference.ts, 15, 32)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 20, 14)) | ||
|
||
num: Result<F, number> | ||
>num : Symbol(Ops.num, Decl(genericFunctionsAndConditionalInference.ts, 22, 26)) | ||
>Result : Symbol(Result, Decl(genericFunctionsAndConditionalInference.ts, 15, 32)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 20, 14)) | ||
|
||
lr<I, O>(a: Result<F, I>, o: Result<F, O>): Result<F, LR<F, I, O>> | ||
>lr : Symbol(Ops.lr, Decl(genericFunctionsAndConditionalInference.ts, 23, 26)) | ||
>I : Symbol(I, Decl(genericFunctionsAndConditionalInference.ts, 24, 7)) | ||
>O : Symbol(O, Decl(genericFunctionsAndConditionalInference.ts, 24, 9)) | ||
>a : Symbol(a, Decl(genericFunctionsAndConditionalInference.ts, 24, 13)) | ||
>Result : Symbol(Result, Decl(genericFunctionsAndConditionalInference.ts, 15, 32)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 20, 14)) | ||
>I : Symbol(I, Decl(genericFunctionsAndConditionalInference.ts, 24, 7)) | ||
>o : Symbol(o, Decl(genericFunctionsAndConditionalInference.ts, 24, 29)) | ||
>Result : Symbol(Result, Decl(genericFunctionsAndConditionalInference.ts, 15, 32)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 20, 14)) | ||
>O : Symbol(O, Decl(genericFunctionsAndConditionalInference.ts, 24, 9)) | ||
>Result : Symbol(Result, Decl(genericFunctionsAndConditionalInference.ts, 15, 32)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 20, 14)) | ||
>LR : Symbol(LR, Decl(genericFunctionsAndConditionalInference.ts, 16, 48)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 20, 14)) | ||
>I : Symbol(I, Decl(genericFunctionsAndConditionalInference.ts, 24, 7)) | ||
>O : Symbol(O, Decl(genericFunctionsAndConditionalInference.ts, 24, 9)) | ||
|
||
dict: <P>(p: {[k in keyof P]: Result<F, P[k]>}) => Result<F, P> | ||
>dict : Symbol(Ops.dict, Decl(genericFunctionsAndConditionalInference.ts, 24, 70)) | ||
>P : Symbol(P, Decl(genericFunctionsAndConditionalInference.ts, 25, 11)) | ||
>p : Symbol(p, Decl(genericFunctionsAndConditionalInference.ts, 25, 14)) | ||
>k : Symbol(k, Decl(genericFunctionsAndConditionalInference.ts, 25, 19)) | ||
>P : Symbol(P, Decl(genericFunctionsAndConditionalInference.ts, 25, 11)) | ||
>Result : Symbol(Result, Decl(genericFunctionsAndConditionalInference.ts, 15, 32)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 20, 14)) | ||
>P : Symbol(P, Decl(genericFunctionsAndConditionalInference.ts, 25, 11)) | ||
>k : Symbol(k, Decl(genericFunctionsAndConditionalInference.ts, 25, 19)) | ||
>Result : Symbol(Result, Decl(genericFunctionsAndConditionalInference.ts, 15, 32)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 20, 14)) | ||
>P : Symbol(P, Decl(genericFunctionsAndConditionalInference.ts, 25, 11)) | ||
} | ||
const left: Ops<"left"> = {} as any | ||
>left : Symbol(left, Decl(genericFunctionsAndConditionalInference.ts, 27, 5)) | ||
>Ops : Symbol(Ops, Decl(genericFunctionsAndConditionalInference.ts, 18, 62)) | ||
|
||
const right: Ops<"right"> = {} as any | ||
>right : Symbol(right, Decl(genericFunctionsAndConditionalInference.ts, 28, 5)) | ||
>Ops : Symbol(Ops, Decl(genericFunctionsAndConditionalInference.ts, 18, 62)) | ||
|
||
const ok = <F extends Target>(at: Ops<F>) => ({lr: at.lr(at.str, at.num)}) | ||
>ok : Symbol(ok, Decl(genericFunctionsAndConditionalInference.ts, 30, 5)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 30, 12)) | ||
>Target : Symbol(Target, Decl(genericFunctionsAndConditionalInference.ts, 14, 1)) | ||
>at : Symbol(at, Decl(genericFunctionsAndConditionalInference.ts, 30, 30)) | ||
>Ops : Symbol(Ops, Decl(genericFunctionsAndConditionalInference.ts, 18, 62)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 30, 12)) | ||
>lr : Symbol(lr, Decl(genericFunctionsAndConditionalInference.ts, 30, 47)) | ||
>at.lr : Symbol(Ops.lr, Decl(genericFunctionsAndConditionalInference.ts, 23, 26)) | ||
>at : Symbol(at, Decl(genericFunctionsAndConditionalInference.ts, 30, 30)) | ||
>lr : Symbol(Ops.lr, Decl(genericFunctionsAndConditionalInference.ts, 23, 26)) | ||
>at.str : Symbol(Ops.str, Decl(genericFunctionsAndConditionalInference.ts, 21, 9)) | ||
>at : Symbol(at, Decl(genericFunctionsAndConditionalInference.ts, 30, 30)) | ||
>str : Symbol(Ops.str, Decl(genericFunctionsAndConditionalInference.ts, 21, 9)) | ||
>at.num : Symbol(Ops.num, Decl(genericFunctionsAndConditionalInference.ts, 22, 26)) | ||
>at : Symbol(at, Decl(genericFunctionsAndConditionalInference.ts, 30, 30)) | ||
>num : Symbol(Ops.num, Decl(genericFunctionsAndConditionalInference.ts, 22, 26)) | ||
|
||
const orphaned = <F extends Target>(at: Ops<F>) => at.dict(ok(at)) | ||
>orphaned : Symbol(orphaned, Decl(genericFunctionsAndConditionalInference.ts, 31, 5)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 31, 18)) | ||
>Target : Symbol(Target, Decl(genericFunctionsAndConditionalInference.ts, 14, 1)) | ||
>at : Symbol(at, Decl(genericFunctionsAndConditionalInference.ts, 31, 36)) | ||
>Ops : Symbol(Ops, Decl(genericFunctionsAndConditionalInference.ts, 18, 62)) | ||
>F : Symbol(F, Decl(genericFunctionsAndConditionalInference.ts, 31, 18)) | ||
>at.dict : Symbol(Ops.dict, Decl(genericFunctionsAndConditionalInference.ts, 24, 70)) | ||
>at : Symbol(at, Decl(genericFunctionsAndConditionalInference.ts, 31, 36)) | ||
>dict : Symbol(Ops.dict, Decl(genericFunctionsAndConditionalInference.ts, 24, 70)) | ||
>ok : Symbol(ok, Decl(genericFunctionsAndConditionalInference.ts, 30, 5)) | ||
>at : Symbol(at, Decl(genericFunctionsAndConditionalInference.ts, 31, 36)) | ||
|
||
const leftOk = ok(left) | ||
>leftOk : Symbol(leftOk, Decl(genericFunctionsAndConditionalInference.ts, 33, 5)) | ||
>ok : Symbol(ok, Decl(genericFunctionsAndConditionalInference.ts, 30, 5)) | ||
>left : Symbol(left, Decl(genericFunctionsAndConditionalInference.ts, 27, 5)) | ||
|
||
const leftOrphaned = orphaned(left) | ||
>leftOrphaned : Symbol(leftOrphaned, Decl(genericFunctionsAndConditionalInference.ts, 34, 5)) | ||
>orphaned : Symbol(orphaned, Decl(genericFunctionsAndConditionalInference.ts, 31, 5)) | ||
>left : Symbol(left, Decl(genericFunctionsAndConditionalInference.ts, 27, 5)) | ||
|
||
const rightOk = ok(right) | ||
>rightOk : Symbol(rightOk, Decl(genericFunctionsAndConditionalInference.ts, 36, 5)) | ||
>ok : Symbol(ok, Decl(genericFunctionsAndConditionalInference.ts, 30, 5)) | ||
>right : Symbol(right, Decl(genericFunctionsAndConditionalInference.ts, 28, 5)) | ||
|
||
const rightOrphaned = orphaned(right) | ||
>rightOrphaned : Symbol(rightOrphaned, Decl(genericFunctionsAndConditionalInference.ts, 37, 5)) | ||
>orphaned : Symbol(orphaned, Decl(genericFunctionsAndConditionalInference.ts, 31, 5)) | ||
>right : Symbol(right, Decl(genericFunctionsAndConditionalInference.ts, 28, 5)) | ||
|
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.
So, the instantiation for a reverse mapped type inferred from some generic source is just another reverse mapped type for the instantiation of that source. Is that the right way to read this?
So, to walk through a slight simplification of Anders’
unboxify
example:At the
unboxify(obj)
call, we infer a reverse mapped type from source{ prop: { value: U } }
to targetT
. Later, at thefoo({ prop: { value: U } })
when we have a mapper fromU
tonumber
, we instantiate the aforementioned reverse mapped type by making another reverse mapped type as if we were performing inference from source{ prop: { value: number } }
to targetT
, which, by the actual reverse mapping mechanism (unchanged in this PR, and I don’t know where it lives or when it’s triggered) to resolve that reverse mapped type into{ prop: number }
. Prior to this PR, basically the same process was happening, but we were trying to resolve the original reverse mapped type with the uninstantiated source, so we would have just gotten{ prop: U }
.I still don’t really understand the instantiations of
type.mappedType
andtype.constraintType
—in the examples I’ve walked through, thoseinstantiateType
calls end up just returning the original type passed to them. E.g., in theunboxify
example, the mapped type and constraint type only have references toT
, and the mapper only mapsU
(which is only relevant to the source), and it’s hard to imagine how the inner mapped type could contain any type variables you’d ever find in the source. Is this only relevant to the special mappers you mentioned to Anders earlier?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.
Or, let me ask a more specific question about
permissiveMapper
and the error scenarios you mentioned—what would be the consequences of, rather than early returning the inputtype
, just proceeding to return the reverse mapped type with an instantiated source, but withmappedType
andconstraintType
simply copied:I think this would have been my instinct, and I don’t understand what would go wrong, in what cases.
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.
Yeah, function type mappers that eliminate all type variables are pretty much the only ones that would affect those inner instantiations. The effect would be those type parameters would go unmapped, so we'd get inaccurate results when we use them. The means that, eg, we'd measure conditional type
extends
checks incorrectly when they involved type parameters at these positions within a reverse mapped type.