Skip to content

Commit ed497c4

Browse files
committed
Properly handle recursive type references in type inference
1 parent 20e2be1 commit ed497c4

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17021,7 +17021,7 @@ namespace ts {
1702117021
function couldContainTypeVariables(type: Type): boolean {
1702217022
const objectFlags = getObjectFlags(type);
1702317023
return !!(type.flags & TypeFlags.Instantiable ||
17024-
objectFlags & ObjectFlags.Reference && forEach(getTypeArguments(<TypeReference>type), couldContainTypeVariables) ||
17024+
objectFlags & ObjectFlags.Reference && ((<TypeReference>type).node || forEach(getTypeArguments(<TypeReference>type), couldContainTypeVariables)) ||
1702517025
objectFlags & ObjectFlags.Anonymous && type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) && type.symbol.declarations ||
1702617026
objectFlags & ObjectFlags.Mapped ||
1702717027
type.flags & TypeFlags.UnionOrIntersection && !(type.flags & TypeFlags.EnumLiteral) && couldUnionOrIntersectionContainTypeVariables(<UnionOrIntersectionType>type));
@@ -17318,7 +17318,8 @@ namespace ts {
1731817318
}
1731917319
}
1732017320
if (getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference && (
17321-
(<TypeReference>source).target === (<TypeReference>target).target || isArrayType(source) && isArrayType(target))) {
17321+
(<TypeReference>source).target === (<TypeReference>target).target || isArrayType(source) && isArrayType(target)) &&
17322+
!((<TypeReference>source).node && (<TypeReference>target).node)) {
1732217323
// If source and target are references to the same generic type, infer from type arguments
1732317324
inferFromTypeArguments(getTypeArguments(<TypeReference>source), getTypeArguments(<TypeReference>target), getVariances((<TypeReference>source).target));
1732417325
}
@@ -17599,6 +17600,12 @@ namespace ts {
1759917600
}
1760017601

1760117602
function inferFromObjectTypesWorker(source: Type, target: Type) {
17603+
if (getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference && (
17604+
(<TypeReference>source).target === (<TypeReference>target).target || isArrayType(source) && isArrayType(target))) {
17605+
// If source and target are references to the same generic type, infer from type arguments
17606+
inferFromTypeArguments(getTypeArguments(<TypeReference>source), getTypeArguments(<TypeReference>target), getVariances((<TypeReference>source).target));
17607+
return;
17608+
}
1760217609
if (isGenericMappedType(source) && isGenericMappedType(target)) {
1760317610
// The source and target types are generic types { [P in S]: X } and { [P in T]: Y }, so we infer
1760417611
// from S to T and from X to Y.

0 commit comments

Comments
 (0)