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 2 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
18 changes: 8 additions & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11217,7 +11217,7 @@ 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, visited: Map<true>): Type {
const properties = getPropertiesOfType(source);
let indexInfo = getIndexInfoOfType(source, IndexKind.String);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this tested anywhere in a more obvious way? e.g.

// @declaration: true

type MyReadonly<T> = { readonly [K in keyof T]: T[K] };

export declare function foo<T>(x: MyReadonly<T>): T;

export let x = foo(100);

if (properties.length === 0 && !indexInfo) {
Expand Down Expand Up @@ -11250,7 +11250,7 @@ namespace ts {

function inferTargetType(sourceType: Type): Type {
inference.candidates = undefined;
inferTypes(inferences, sourceType, templateType, 0, mappedTypeStack);
inferTypes(inferences, sourceType, templateType, 0, visited);
return inference.candidates ? getUnionType(inference.candidates, /*subtypeReduction*/ true) : emptyObjectType;
}
}
Expand All @@ -11268,9 +11268,8 @@ 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, visited?: Map<true>) {
let symbolStack: Symbol[];
let visited: Map<boolean>;
inferFromTypes(originalSource, originalTarget);

function inferFromTypes(source: Type, target: Type) {
Expand Down Expand Up @@ -11424,7 +11423,7 @@ namespace ts {
if (visited && visited.get(key)) {
return;
}
(visited || (visited = createMap<boolean>())).set(key, true);
(visited || (visited = createMap<true>())).set(key, true);
// If we are already processing another target type with the same associated symbol (such as
// an instantiation of the same generic type), we do not explore this target as it would yield
// no further inferences. We exclude the static side of classes from this check since it shares
Expand Down Expand Up @@ -11485,13 +11484,12 @@ 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)) {
const key = (source.symbol ? getSymbolId(source.symbol) + "s" : "") + getSymbolId(target.symbol);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally we distinguish different types of cache keys with a leading character, rather than a different separator, see getLiteralType for prior art. Plus, I think something like T123,12 vs S11,14 is going to make more sense in the debugger than 123,12 vs 11s14.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Done.

if (visited && visited.has(key)) {
return;
}
(mappedTypeStack || (mappedTypeStack = [])).push(key);
const inferredType = inferTypeForHomomorphicMappedType(source, <MappedType>target, mappedTypeStack);
mappedTypeStack.pop();
(visited || (visited = createMap<true>())).set(key, true);
const inferredType = inferTypeForHomomorphicMappedType(source, <MappedType>target, visited);
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);