Skip to content

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
merged 4 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15908,6 +15908,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;
Expand Down Expand Up @@ -15958,6 +15961,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
);
Comment on lines +15973 to +15977
Copy link
Member

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:

type Boxified<T> = { [P in keyof T]: { value: T[P] } };

declare function unboxify<T>(obj: Boxified<T>): T;

function foo<U>(obj: { prop: { value: U }  }) {
    return unboxify(obj);
}

foo({ prop: { value: 10 } });

At the unboxify(obj) call, we infer a reverse mapped type from source { prop: { value: U } } to target T. Later, at the foo({ prop: { value: U } }) when we have a mapper from U to number, 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 target T, 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 and type.constraintType—in the examples I’ve walked through, those instantiateType calls end up just returning the original type passed to them. E.g., in the unboxify example, the mapped type and constraint type only have references to T, and the mapper only maps U (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?

Copy link
Member

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 input type, just proceeding to return the reverse mapped type with an instantiated source, but with mappedType and constraintType simply copied:

function instantiateReverseMappedType(type: ReverseMappedType, mapper: TypeMapper) {
     return inferTypeForHomomorphicMappedType(
         instantiateType(type.source, mapper),
         type.mappedType,
         type.constraintType
     ) || type;
}

I think this would have been my instinct, and I don’t understand what would go wrong, in what cases.

Copy link
Member Author

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.

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));
Expand Down Expand Up @@ -20328,7 +20351,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);
Expand Down
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);
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))

Loading