Skip to content

Commit ec446b6

Browse files
Andaristgabritto
andauthored
Fixed crash on circular local type arguments when outer ones are present too (#59089)
Co-authored-by: Gabriela Araujo Britto <[email protected]>
1 parent 652c96c commit ec446b6

5 files changed

+61
-2
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16300,7 +16300,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1630016300
function getTypeArguments(type: TypeReference): readonly Type[] {
1630116301
if (!type.resolvedTypeArguments) {
1630216302
if (!pushTypeResolution(type, TypeSystemPropertyName.ResolvedTypeArguments)) {
16303-
return type.target.localTypeParameters?.map(() => errorType) || emptyArray;
16303+
return concatenate(type.target.outerTypeParameters, type.target.localTypeParameters?.map(() => errorType)) || emptyArray;
1630416304
}
1630516305
const node = type.node;
1630616306
const typeArguments = !node ? emptyArray :
@@ -16311,7 +16311,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1631116311
type.resolvedTypeArguments ??= type.mapper ? instantiateTypes(typeArguments, type.mapper) : typeArguments;
1631216312
}
1631316313
else {
16314-
type.resolvedTypeArguments ??= type.target.localTypeParameters?.map(() => errorType) || emptyArray;
16314+
type.resolvedTypeArguments ??= concatenate(type.target.outerTypeParameters, type.target.localTypeParameters?.map(() => errorType) || emptyArray);
1631516315
error(
1631616316
type.node || currentNode,
1631716317
type.target.symbol ? Diagnostics.Type_arguments_for_0_circularly_reference_themselves : Diagnostics.Tuple_type_arguments_circularly_reference_themselves,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
circularTypeArgumentsLocalAndOuterNoCrash1.ts(5,12): error TS4109: Type arguments for 'NumArray' circularly reference themselves.
2+
3+
4+
==== circularTypeArgumentsLocalAndOuterNoCrash1.ts (1 errors) ====
5+
// https://github.com/microsoft/TypeScript/issues/59062
6+
7+
function f<_T, _S>() {
8+
interface NumArray<T extends number> extends Array<T> {}
9+
type X = NumArray<X extends {} ? number : number>;
10+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
!!! error TS4109: Type arguments for 'NumArray' circularly reference themselves.
12+
}
13+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/circularTypeArgumentsLocalAndOuterNoCrash1.ts] ////
2+
3+
=== circularTypeArgumentsLocalAndOuterNoCrash1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/59062
5+
6+
function f<_T, _S>() {
7+
>f : Symbol(f, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 0, 0))
8+
>_T : Symbol(_T, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 2, 11))
9+
>_S : Symbol(_S, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 2, 14))
10+
11+
interface NumArray<T extends number> extends Array<T> {}
12+
>NumArray : Symbol(NumArray, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 2, 22))
13+
>T : Symbol(T, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 3, 21))
14+
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
15+
>T : Symbol(T, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 3, 21))
16+
17+
type X = NumArray<X extends {} ? number : number>;
18+
>X : Symbol(X, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 3, 58))
19+
>NumArray : Symbol(NumArray, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 2, 22))
20+
>X : Symbol(X, Decl(circularTypeArgumentsLocalAndOuterNoCrash1.ts, 3, 58))
21+
}
22+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/compiler/circularTypeArgumentsLocalAndOuterNoCrash1.ts] ////
2+
3+
=== circularTypeArgumentsLocalAndOuterNoCrash1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/59062
5+
6+
function f<_T, _S>() {
7+
>f : <_T, _S>() => void
8+
> : ^ ^^ ^^^^^^^^^^^
9+
10+
interface NumArray<T extends number> extends Array<T> {}
11+
type X = NumArray<X extends {} ? number : number>;
12+
>X : NumArray<any>
13+
> : ^^^^^^^^^^^^^
14+
}
15+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/59062
5+
6+
function f<_T, _S>() {
7+
interface NumArray<T extends number> extends Array<T> {}
8+
type X = NumArray<X extends {} ? number : number>;
9+
}

0 commit comments

Comments
 (0)