Skip to content

Cache inference for recursive mapped types #20622

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

Closed
wants to merge 8 commits into from
Closed
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
33 changes: 22 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11221,7 +11221,10 @@ namespace ts {
* property is computed by inferring from the source property type to X for the type
* variable T[P] (i.e. we treat the type T[P] as the type variable we're inferring for).
*/
function inferTypeForHomomorphicMappedType(source: Type, target: MappedType, mappedTypeStack: string[]): Type {
function inferTypeForHomomorphicMappedType(source: Type, target: MappedType, mappedTypeCache: Map<Type | undefined>): Type {
if (source.flags & TypeFlags.Primitive) {
return source;
}
const properties = getPropertiesOfType(source);
let indexInfo = getIndexInfoOfType(source, IndexKind.String);
if (properties.length === 0 && !indexInfo) {
Expand Down Expand Up @@ -11254,7 +11257,7 @@ namespace ts {

function inferTargetType(sourceType: Type): Type {
inference.candidates = undefined;
inferTypes(inferences, sourceType, templateType, 0, mappedTypeStack);
inferTypes(inferences, sourceType, templateType, 0, mappedTypeCache);
return inference.candidates ? getUnionType(inference.candidates, UnionReduction.Subtype) : emptyObjectType;
}
}
Expand All @@ -11272,7 +11275,7 @@ namespace ts {
return undefined;
}

function inferTypes(inferences: InferenceInfo[], originalSource: Type, originalTarget: Type, priority: InferencePriority = 0, mappedTypeStack?: string[]) {
function inferTypes(inferences: InferenceInfo[], originalSource: Type, originalTarget: Type, priority: InferencePriority = 0, mappedTypeCache?: Map<Type | undefined>) {
let symbolStack: Symbol[];
let visited: Map<boolean>;
inferFromTypes(originalSource, originalTarget);
Expand Down Expand Up @@ -11422,10 +11425,15 @@ namespace ts {
}
}
else {
source = getApparentType(source);
if (getObjectFlags(target) & ObjectFlags.Mapped && source.flags & TypeFlags.Primitive) {
inferFromObjectTypes(source, target);
}
else {
source = getApparentType(source);
}
if (source.flags & (TypeFlags.Object | TypeFlags.Intersection)) {
const key = source.id + "," + target.id;
if (visited && visited.get(key)) {
if (visited && visited.has(key)) {
return;
}
(visited || (visited = createMap<boolean>())).set(key, true);
Expand Down Expand Up @@ -11489,13 +11497,16 @@ namespace ts {
// such that direct inferences to T get priority over inferences to Partial<T>, for example.
const inference = getInferenceInfoForType((<IndexType>constraintType).type);
if (inference && !inference.isFixed) {
const key = (source.symbol ? getSymbolId(source.symbol) + "," : "") + getSymbolId(target.symbol);
if (contains(mappedTypeStack, key)) {
return;
const key = (source.symbol ? "S" + getSymbolId(source.symbol) : "T" + source.id) + ",S" + getSymbolId(target.symbol);
let inferredType: Type;
if (mappedTypeCache && mappedTypeCache.has(key)) {
inferredType = mappedTypeCache.get(key);
}
else {
(mappedTypeCache || (mappedTypeCache = createMap<Type | undefined>())).set(key, undefined);
inferredType = inferTypeForHomomorphicMappedType(source, <MappedType>target, mappedTypeCache);
mappedTypeCache.set(key, inferredType);
}
(mappedTypeStack || (mappedTypeStack = [])).push(key);
const inferredType = inferTypeForHomomorphicMappedType(source, <MappedType>target, mappedTypeStack);
mappedTypeStack.pop();
if (inferredType) {
const savePriority = priority;
priority |= InferencePriority.MappedType;
Expand Down
5 changes: 5 additions & 0 deletions tests/baselines/reference/mappedTypeRecursiveInference.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ declare let a: A;
type Deep<T> = { [K in keyof T]: Deep<T[K]> }
declare function foo<T>(deep: Deep<T>): T;
const out = foo(a);

let xhr: XMLHttpRequest;
const out2 = foo(xhr);


//// [mappedTypeRecursiveInference.js]
var out = foo(a);
var xhr;
var out2 = foo(xhr);
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ const out = foo(a);
>foo : Symbol(foo, Decl(mappedTypeRecursiveInference.ts, 2, 45))
>a : Symbol(a, Decl(mappedTypeRecursiveInference.ts, 1, 11))

let xhr: XMLHttpRequest;
>xhr : Symbol(xhr, Decl(mappedTypeRecursiveInference.ts, 6, 3))
>XMLHttpRequest : Symbol(XMLHttpRequest, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))

const out2 = foo(xhr);
>out2 : Symbol(out2, Decl(mappedTypeRecursiveInference.ts, 7, 5))
>foo : Symbol(foo, Decl(mappedTypeRecursiveInference.ts, 2, 45))
>xhr : Symbol(xhr, Decl(mappedTypeRecursiveInference.ts, 6, 3))

10 changes: 10 additions & 0 deletions tests/baselines/reference/mappedTypeRecursiveInference.types

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions tests/cases/compiler/mappedTypeRecursiveInference.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// @lib: es6, dom
interface A { a: A }
declare let a: A;
type Deep<T> = { [K in keyof T]: Deep<T[K]> }
declare function foo<T>(deep: Deep<T>): T;
const out = foo(a);

let xhr: XMLHttpRequest;
const out2 = foo(xhr);