Add instantiation rules for reverse mapped types (#42449)

* Add instantiation rules for reverse mapped types

* Add smaller example of same issue
This commit is contained in:
Wesley Wigham
2021-04-27 15:01:46 -07:00
committed by GitHub
parent cfb9a25cc5
commit 5e4fcfbfb6
5 changed files with 442 additions and 1 deletions

View File

@@ -16158,6 +16158,9 @@ namespace ts {
const newTypeArguments = instantiateTypes(resolvedTypeArguments, mapper);
return newTypeArguments !== resolvedTypeArguments ? createNormalizedTypeReference((<TypeReference>type).target, newTypeArguments) : type;
}
if (objectFlags & ObjectFlags.ReverseMapped) {
return instantiateReverseMappedType(type as ReverseMappedType, mapper);
}
return getObjectTypeInstantiation(<TypeReference | AnonymousType | MappedType>type, mapper, aliasSymbol, aliasTypeArguments);
}
return type;
@@ -16208,6 +16211,26 @@ namespace ts {
return type;
}
function instantiateReverseMappedType(type: ReverseMappedType, mapper: TypeMapper) {
const innerMappedType = instantiateType(type.mappedType, mapper);
if (!(getObjectFlags(innerMappedType) & ObjectFlags.Mapped)) {
return type;
}
const innerIndexType = instantiateType(type.constraintType, mapper);
if (!(innerIndexType.flags & TypeFlags.Index)) {
return type;
}
const instantiated = inferTypeForHomomorphicMappedType(
instantiateType(type.source, mapper),
innerMappedType as MappedType,
innerIndexType as IndexType
);
if (instantiated) {
return instantiated;
}
return type; // Nested invocation of `inferTypeForHomomorphicMappedType` or the `source` instantiated into something unmappable
}
function getPermissiveInstantiation(type: Type) {
return type.flags & (TypeFlags.Primitive | TypeFlags.AnyOrUnknown | TypeFlags.Never) ? type :
type.permissiveInstantiation || (type.permissiveInstantiation = instantiateType(type, permissiveMapper));
@@ -20674,7 +20697,7 @@ namespace ts {
type.flags & TypeFlags.Object && !isNonGenericTopLevelType(type) && (
objectFlags & ObjectFlags.Reference && ((<TypeReference>type).node || forEach(getTypeArguments(<TypeReference>type), couldContainTypeVariables)) ||
objectFlags & ObjectFlags.Anonymous && type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) && type.symbol.declarations ||
objectFlags & (ObjectFlags.Mapped | ObjectFlags.ObjectRestType)) ||
objectFlags & (ObjectFlags.Mapped | ObjectFlags.ReverseMapped | ObjectFlags.ObjectRestType)) ||
type.flags & TypeFlags.UnionOrIntersection && !(type.flags & TypeFlags.EnumLiteral) && !isNonGenericTopLevelType(type) && some((<UnionOrIntersectionType>type).types, couldContainTypeVariables));
if (type.flags & TypeFlags.ObjectFlagsType) {
(<ObjectFlagsType>type).objectFlags |= ObjectFlags.CouldContainTypeVariablesComputed | (result ? ObjectFlags.CouldContainTypeVariables : 0);

View File

@@ -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);

View File

@@ -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))

View File

@@ -0,0 +1,137 @@
=== tests/cases/compiler/genericFunctionsAndConditionalInference.ts ===
type Boxified<T> = { [P in keyof T]: { value: T[P]} };
>Boxified : Boxified<T>
>value : T[P]
declare function unboxify<T>(obj: Boxified<T>): T;
>unboxify : <T>(obj: Boxified<T>) => T
>obj : Boxified<T>
function foo<U, V>(obj: { u: { value: U }, v: { value: V } }) {
>foo : <U, V>(obj: { u: { value: U; }; v: { value: V; };}) => { u: U; v: V; }
>obj : { u: { value: U;}; v: { value: V;}; }
>u : { value: U; }
>value : U
>v : { value: V; }
>value : V
return unboxify(obj);
>unboxify(obj) : { u: U; v: V; }
>unboxify : <T>(obj: Boxified<T>) => T
>obj : { u: { value: U; }; v: { value: V; }; }
}
let qq = foo({ u: { value: 10 }, v: { value: 'hello'} }); // { u: U, v: V } but should be { u: number, v: string }
>qq : { u: number; v: string; }
>foo({ u: { value: 10 }, v: { value: 'hello'} }) : { u: number; v: string; }
>foo : <U, V>(obj: { u: { value: U; }; v: { value: V; }; }) => { u: U; v: V; }
>{ u: { value: 10 }, v: { value: 'hello'} } : { u: { value: number; }; v: { value: string; }; }
>u : { value: number; }
>{ value: 10 } : { value: number; }
>value : number
>10 : 10
>v : { value: string; }
>{ value: 'hello'} : { value: string; }
>value : string
>'hello' : "hello"
// From #42385
interface Targets<A> {
left: A
>left : A
right: A
>right : A
}
type Target = keyof Targets<any>
>Target : keyof Targets<any>
type Result<F extends Target, A> = Targets<A>[F]
>Result : Result<F, A>
type LR<F extends Target, L, R> = [F] extends ["left"] ? L : R
>LR : LR<F, L, R>
interface Ops<F extends Target> {
_f: F
>_f : F
str: Result<F, string>
>str : Result<F, string>
num: Result<F, number>
>num : Result<F, number>
lr<I, O>(a: Result<F, I>, o: Result<F, O>): Result<F, LR<F, I, O>>
>lr : <I, O>(a: Result<F, I>, o: Result<F, O>) => Result<F, LR<F, I, O>>
>a : Result<F, I>
>o : Result<F, O>
dict: <P>(p: {[k in keyof P]: Result<F, P[k]>}) => Result<F, P>
>dict : <P>(p: { [k in keyof P]: Result<F, P[k]>; }) => Result<F, P>
>p : { [k in keyof P]: Result<F, P[k]>; }
}
const left: Ops<"left"> = {} as any
>left : Ops<"left">
>{} as any : any
>{} : {}
const right: Ops<"right"> = {} as any
>right : Ops<"right">
>{} as any : any
>{} : {}
const ok = <F extends Target>(at: Ops<F>) => ({lr: at.lr(at.str, at.num)})
>ok : <F extends keyof Targets<any>>(at: Ops<F>) => { lr: Result<F, LR<F, string, number>>; }
><F extends Target>(at: Ops<F>) => ({lr: at.lr(at.str, at.num)}) : <F extends keyof Targets<any>>(at: Ops<F>) => { lr: Result<F, LR<F, string, number>>; }
>at : Ops<F>
>({lr: at.lr(at.str, at.num)}) : { lr: Result<F, LR<F, string, number>>; }
>{lr: at.lr(at.str, at.num)} : { lr: Result<F, LR<F, string, number>>; }
>lr : Result<F, LR<F, string, number>>
>at.lr(at.str, at.num) : Result<F, LR<F, string, number>>
>at.lr : <I, O>(a: Result<F, I>, o: Result<F, O>) => Result<F, LR<F, I, O>>
>at : Ops<F>
>lr : <I, O>(a: Result<F, I>, o: Result<F, O>) => Result<F, LR<F, I, O>>
>at.str : Result<F, string>
>at : Ops<F>
>str : Result<F, string>
>at.num : Result<F, number>
>at : Ops<F>
>num : Result<F, number>
const orphaned = <F extends Target>(at: Ops<F>) => at.dict(ok(at))
>orphaned : <F extends keyof Targets<any>>(at: Ops<F>) => Result<F, { lr: LR<F, string, number>; }>
><F extends Target>(at: Ops<F>) => at.dict(ok(at)) : <F extends keyof Targets<any>>(at: Ops<F>) => Result<F, { lr: LR<F, string, number>; }>
>at : Ops<F>
>at.dict(ok(at)) : Result<F, { lr: LR<F, string, number>; }>
>at.dict : <P>(p: { [k in keyof P]: Result<F, P[k]>; }) => Result<F, P>
>at : Ops<F>
>dict : <P>(p: { [k in keyof P]: Result<F, P[k]>; }) => Result<F, P>
>ok(at) : { lr: Result<F, LR<F, string, number>>; }
>ok : <F extends keyof Targets<any>>(at: Ops<F>) => { lr: Result<F, LR<F, string, number>>; }
>at : Ops<F>
const leftOk = ok(left)
>leftOk : { lr: string; }
>ok(left) : { lr: string; }
>ok : <F extends keyof Targets<any>>(at: Ops<F>) => { lr: Result<F, LR<F, string, number>>; }
>left : Ops<"left">
const leftOrphaned = orphaned(left)
>leftOrphaned : { lr: string; }
>orphaned(left) : { lr: string; }
>orphaned : <F extends keyof Targets<any>>(at: Ops<F>) => Result<F, { lr: LR<F, string, number>; }>
>left : Ops<"left">
const rightOk = ok(right)
>rightOk : { lr: number; }
>ok(right) : { lr: number; }
>ok : <F extends keyof Targets<any>>(at: Ops<F>) => { lr: Result<F, LR<F, string, number>>; }
>right : Ops<"right">
const rightOrphaned = orphaned(right)
>rightOrphaned : { lr: number; }
>orphaned(right) : { lr: number; }
>orphaned : <F extends keyof Targets<any>>(at: Ops<F>) => Result<F, { lr: LR<F, string, number>; }>
>right : Ops<"right">

View File

@@ -0,0 +1,38 @@
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)