Skip to content

Cut off inference for recursive mapped types #20370

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 5 commits into from
Dec 1, 2017
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
14 changes: 10 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11127,7 +11127,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): Type {
function inferTypeForHomomorphicMappedType(source: Type, target: MappedType, mappedTypeStack: string[]): Type {
const properties = getPropertiesOfType(source);
let indexInfo = getIndexInfoOfType(source, IndexKind.String);
if (properties.length === 0 && !indexInfo) {
Expand Down Expand Up @@ -11160,7 +11160,7 @@ namespace ts {

function inferTargetType(sourceType: Type): Type {
inference.candidates = undefined;
inferTypes(inferences, sourceType, templateType);
inferTypes(inferences, sourceType, templateType, 0, mappedTypeStack);
return inference.candidates ? getUnionType(inference.candidates, /*subtypeReduction*/ true) : emptyObjectType;
}
}
Expand All @@ -11178,7 +11178,7 @@ namespace ts {
return undefined;
}

function inferTypes(inferences: InferenceInfo[], originalSource: Type, originalTarget: Type, priority: InferencePriority = 0) {
function inferTypes(inferences: InferenceInfo[], originalSource: Type, originalTarget: Type, priority: InferencePriority = 0, mappedTypeStack?: string[]) {
let symbolStack: Symbol[];
let visited: Map<boolean>;
inferFromTypes(originalSource, originalTarget);
Expand Down Expand Up @@ -11395,7 +11395,13 @@ 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 inferredType = inferTypeForHomomorphicMappedType(source, <MappedType>target);
const key = (source.symbol ? getSymbolId(source.symbol) + "," : "") + getSymbolId(target.symbol);
if (contains(mappedTypeStack, key)) {
return;
}
(mappedTypeStack || (mappedTypeStack = [])).push(key);
const inferredType = inferTypeForHomomorphicMappedType(source, <MappedType>target, mappedTypeStack);
mappedTypeStack.pop();
if (inferredType) {
const savePriority = priority;
priority |= InferencePriority.MappedType;
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/mappedTypeRecursiveInference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [mappedTypeRecursiveInference.ts]
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);


//// [mappedTypeRecursiveInference.js]
var out = foo(a);
32 changes: 32 additions & 0 deletions tests/baselines/reference/mappedTypeRecursiveInference.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/compiler/mappedTypeRecursiveInference.ts ===
interface A { a: A }
>A : Symbol(A, Decl(mappedTypeRecursiveInference.ts, 0, 0))
>a : Symbol(A.a, Decl(mappedTypeRecursiveInference.ts, 0, 13))
>A : Symbol(A, Decl(mappedTypeRecursiveInference.ts, 0, 0))

declare let a: A;
>a : Symbol(a, Decl(mappedTypeRecursiveInference.ts, 1, 11))
>A : Symbol(A, Decl(mappedTypeRecursiveInference.ts, 0, 0))

type Deep<T> = { [K in keyof T]: Deep<T[K]> }
>Deep : Symbol(Deep, Decl(mappedTypeRecursiveInference.ts, 1, 17))
>T : Symbol(T, Decl(mappedTypeRecursiveInference.ts, 2, 10))
>K : Symbol(K, Decl(mappedTypeRecursiveInference.ts, 2, 18))
>T : Symbol(T, Decl(mappedTypeRecursiveInference.ts, 2, 10))
>Deep : Symbol(Deep, Decl(mappedTypeRecursiveInference.ts, 1, 17))
>T : Symbol(T, Decl(mappedTypeRecursiveInference.ts, 2, 10))
>K : Symbol(K, Decl(mappedTypeRecursiveInference.ts, 2, 18))

declare function foo<T>(deep: Deep<T>): T;
>foo : Symbol(foo, Decl(mappedTypeRecursiveInference.ts, 2, 45))
>T : Symbol(T, Decl(mappedTypeRecursiveInference.ts, 3, 21))
>deep : Symbol(deep, Decl(mappedTypeRecursiveInference.ts, 3, 24))
>Deep : Symbol(Deep, Decl(mappedTypeRecursiveInference.ts, 1, 17))
>T : Symbol(T, Decl(mappedTypeRecursiveInference.ts, 3, 21))
>T : Symbol(T, Decl(mappedTypeRecursiveInference.ts, 3, 21))

const out = foo(a);
>out : Symbol(out, Decl(mappedTypeRecursiveInference.ts, 4, 5))
>foo : Symbol(foo, Decl(mappedTypeRecursiveInference.ts, 2, 45))
>a : Symbol(a, Decl(mappedTypeRecursiveInference.ts, 1, 11))

33 changes: 33 additions & 0 deletions tests/baselines/reference/mappedTypeRecursiveInference.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=== tests/cases/compiler/mappedTypeRecursiveInference.ts ===
interface A { a: A }
>A : A
>a : A
>A : A

declare let a: A;
>a : A
>A : A

type Deep<T> = { [K in keyof T]: Deep<T[K]> }
>Deep : Deep<T>
>T : T
>K : K
>T : T
>Deep : Deep<T>
>T : T
>K : K

declare function foo<T>(deep: Deep<T>): T;
>foo : <T>(deep: Deep<T>) => T
>T : T
>deep : Deep<T>
>Deep : Deep<T>
>T : T
>T : T

const out = foo(a);
>out : { a: {}; }
>foo(a) : { a: {}; }
>foo : <T>(deep: Deep<T>) => T
>a : A

5 changes: 5 additions & 0 deletions tests/cases/compiler/mappedTypeRecursiveInference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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);