Skip to content

Commit b377e99

Browse files
authored
Prevent infinite recursion resolving nested conditional types with import types in them (microsoft#32097)
* Prevent infinite recursion resolving conditional types * Use push/popTypeResolution and issue error * Add failing test * Fix the actual problem * Revert unnecessary changes
1 parent 3f75d2c commit b377e99

5 files changed

+63
-3
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5037,8 +5037,10 @@ namespace ts {
50375037
return Debug.assertNever(propertyName);
50385038
}
50395039

5040-
// Pop an entry from the type resolution stack and return its associated result value. The result value will
5041-
// be true if no circularities were detected, or false if a circularity was found.
5040+
/**
5041+
* Pop an entry from the type resolution stack and return its associated result value. The result value will
5042+
* be true if no circularities were detected, or false if a circularity was found.
5043+
*/
50425044
function popTypeResolution(): boolean {
50435045
resolutionTargets.pop();
50445046
resolutionPropertyNames.pop();
@@ -11402,7 +11404,8 @@ namespace ts {
1140211404

1140311405
function maybeTypeParameterReference(node: Node) {
1140411406
return !(node.kind === SyntaxKind.QualifiedName ||
11405-
node.parent.kind === SyntaxKind.TypeReference && (<TypeReferenceNode>node.parent).typeArguments && node === (<TypeReferenceNode>node.parent).typeName);
11407+
node.parent.kind === SyntaxKind.TypeReference && (<TypeReferenceNode>node.parent).typeArguments && node === (<TypeReferenceNode>node.parent).typeName ||
11408+
node.parent.kind === SyntaxKind.ImportType && (node.parent as ImportTypeNode).typeArguments && node === (node.parent as ImportTypeNode).qualifier);
1140611409
}
1140711410

1140811411
function isTypeParameterPossiblyReferenced(tp: TypeParameter, node: Node) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/nestedGenericConditionalTypeWithGenericImportType.ts] ////
2+
3+
//// [name.ts]
4+
// #31824
5+
6+
export type Name<T> = any;
7+
8+
//// [index.ts]
9+
type T<X> = any extends ((any extends any ? any : string) extends any ? import("./name").Name<X> : any)
10+
? any
11+
: any;
12+
13+
14+
//// [name.js]
15+
"use strict";
16+
// #31824
17+
exports.__esModule = true;
18+
//// [index.js]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/name.ts ===
2+
// #31824
3+
4+
export type Name<T> = any;
5+
>Name : Symbol(Name, Decl(name.ts, 0, 0))
6+
>T : Symbol(T, Decl(name.ts, 2, 17))
7+
8+
=== tests/cases/compiler/index.ts ===
9+
type T<X> = any extends ((any extends any ? any : string) extends any ? import("./name").Name<X> : any)
10+
>T : Symbol(T, Decl(index.ts, 0, 0))
11+
>X : Symbol(X, Decl(index.ts, 0, 7))
12+
>Name : Symbol(Name, Decl(name.ts, 0, 0))
13+
>X : Symbol(X, Decl(index.ts, 0, 7))
14+
15+
? any
16+
: any;
17+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/name.ts ===
2+
// #31824
3+
4+
export type Name<T> = any;
5+
>Name : any
6+
7+
=== tests/cases/compiler/index.ts ===
8+
type T<X> = any extends ((any extends any ? any : string) extends any ? import("./name").Name<X> : any)
9+
>T : any
10+
11+
? any
12+
: any;
13+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// #31824
2+
3+
// @filename: name.ts
4+
export type Name<T> = any;
5+
6+
// @filename: index.ts
7+
type T<X> = any extends ((any extends any ? any : string) extends any ? import("./name").Name<X> : any)
8+
? any
9+
: any;

0 commit comments

Comments
 (0)