Skip to content

Commit 90648fd

Browse files
authored
Merge pull request #14991 from Microsoft/fixTypeRelationStackOverflow
Fix type relation stack overflow
2 parents c62cc3f + 854731a commit 90648fd

13 files changed

+368
-262
lines changed

src/compiler/checker.ts

+163-154
Large diffs are not rendered by default.

tests/baselines/reference/arrayOfSubtypeIsAssignableToReadonlyArray.errors.txt

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(18,1): error T
88
Types of property 'concat' are incompatible.
99
Type '{ (...items: A[][]): A[]; (...items: (A | A[])[]): A[]; }' is not assignable to type '{ <U extends ReadonlyArray<B>>(...items: U[]): B[]; (...items: B[][]): B[]; (...items: (B | B[])[]): B[]; }'.
1010
Type 'A[]' is not assignable to type 'B[]'.
11-
Type 'A' is not assignable to type 'B'.
1211

1312

1413
==== tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts (2 errors) ====
@@ -42,5 +41,4 @@ tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(18,1): error T
4241
!!! error TS2322: Types of property 'concat' are incompatible.
4342
!!! error TS2322: Type '{ (...items: A[][]): A[]; (...items: (A | A[])[]): A[]; }' is not assignable to type '{ <U extends ReadonlyArray<B>>(...items: U[]): B[]; (...items: B[][]): B[]; (...items: (B | B[])[]): B[]; }'.
4443
!!! error TS2322: Type 'A[]' is not assignable to type 'B[]'.
45-
!!! error TS2322: Type 'A' is not assignable to type 'B'.
4644

tests/baselines/reference/mappedTypeRelationships.errors.txt

+52-76
Large diffs are not rendered by default.

tests/baselines/reference/promisePermutations.errors.txt

-6
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ tests/cases/compiler/promisePermutations.ts(133,19): error TS2345: Argument of t
4444
tests/cases/compiler/promisePermutations.ts(134,19): error TS2345: Argument of type '<T>(x: T, cb: <U>(a: U) => U) => Promise<T>' is not assignable to parameter of type '(value: number) => IPromise<any>'.
4545
tests/cases/compiler/promisePermutations.ts(137,11): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
4646
Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
47-
Type 'string' is not assignable to type 'number'.
4847
tests/cases/compiler/promisePermutations.ts(144,12): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
4948
Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
50-
Type 'string' is not assignable to type 'number'.
5149
tests/cases/compiler/promisePermutations.ts(152,12): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
5250
Type argument candidate 'Promise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
5351
Types of property 'then' are incompatible.
@@ -61,7 +59,6 @@ tests/cases/compiler/promisePermutations.ts(156,21): error TS2345: Argument of t
6159
Type 'number' is not assignable to type 'string'.
6260
tests/cases/compiler/promisePermutations.ts(158,21): error TS2345: Argument of type '{ (x: number): IPromise<number>; (x: string): IPromise<string>; }' is not assignable to parameter of type '(value: number) => IPromise<string>'.
6361
Type 'IPromise<number>' is not assignable to type 'IPromise<string>'.
64-
Type 'number' is not assignable to type 'string'.
6562
tests/cases/compiler/promisePermutations.ts(159,21): error TS2345: Argument of type '{ (x: number): Promise<number>; (x: string): Promise<string>; }' is not assignable to parameter of type '(value: number) => Promise<string>'.
6663
Type 'Promise<number>' is not assignable to type 'Promise<string>'.
6764
Type 'number' is not assignable to type 'string'.
@@ -286,7 +283,6 @@ tests/cases/compiler/promisePermutations.ts(160,21): error TS2345: Argument of t
286283
~~~~~~~
287284
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
288285
!!! error TS2453: Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
289-
!!! error TS2453: Type 'string' is not assignable to type 'number'.
290286
var s9g = s9.then(testFunction, nIPromise, sIPromise).then(sIPromise, sIPromise, sIPromise); // ok
291287

292288
var r10 = testFunction10(x => x);
@@ -297,7 +293,6 @@ tests/cases/compiler/promisePermutations.ts(160,21): error TS2345: Argument of t
297293
~~~~~~~~
298294
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
299295
!!! error TS2453: Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
300-
!!! error TS2453: Type 'string' is not assignable to type 'number'.
301296
var r10e = r10.then(testFunction, nIPromise, sIPromise).then(sIPromise, sIPromise, sIPromise); // ok
302297
var s10 = testFunction10P(x => x);
303298
var s10a = s10.then(testFunction10, testFunction10, testFunction10); // ok
@@ -328,7 +323,6 @@ tests/cases/compiler/promisePermutations.ts(160,21): error TS2345: Argument of t
328323
~~~~~~~~~~~~~~
329324
!!! error TS2345: Argument of type '{ (x: number): IPromise<number>; (x: string): IPromise<string>; }' is not assignable to parameter of type '(value: number) => IPromise<string>'.
330325
!!! error TS2345: Type 'IPromise<number>' is not assignable to type 'IPromise<string>'.
331-
!!! error TS2345: Type 'number' is not assignable to type 'string'.
332326
var s11b = s11.then(testFunction11P, testFunction11P, testFunction11P); // error
333327
~~~~~~~~~~~~~~~
334328
!!! error TS2345: Argument of type '{ (x: number): Promise<number>; (x: string): Promise<string>; }' is not assignable to parameter of type '(value: number) => Promise<string>'.

tests/baselines/reference/promisePermutations2.errors.txt

-6
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ tests/cases/compiler/promisePermutations2.ts(132,19): error TS2345: Argument of
4444
tests/cases/compiler/promisePermutations2.ts(133,19): error TS2345: Argument of type '<T>(x: T, cb: <U>(a: U) => U) => Promise<T>' is not assignable to parameter of type '(value: number) => IPromise<any>'.
4545
tests/cases/compiler/promisePermutations2.ts(136,11): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
4646
Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
47-
Type 'string' is not assignable to type 'number'.
4847
tests/cases/compiler/promisePermutations2.ts(143,12): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
4948
Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
50-
Type 'string' is not assignable to type 'number'.
5149
tests/cases/compiler/promisePermutations2.ts(151,12): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
5250
Type argument candidate 'Promise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
5351
Types of property 'then' are incompatible.
@@ -61,7 +59,6 @@ tests/cases/compiler/promisePermutations2.ts(155,21): error TS2345: Argument of
6159
Type 'number' is not assignable to type 'string'.
6260
tests/cases/compiler/promisePermutations2.ts(157,21): error TS2345: Argument of type '{ (x: number): IPromise<number>; (x: string): IPromise<string>; }' is not assignable to parameter of type '(value: number) => IPromise<string>'.
6361
Type 'IPromise<number>' is not assignable to type 'IPromise<string>'.
64-
Type 'number' is not assignable to type 'string'.
6562
tests/cases/compiler/promisePermutations2.ts(158,21): error TS2345: Argument of type '{ (x: number): Promise<number>; (x: string): Promise<string>; }' is not assignable to parameter of type '(value: number) => Promise<string>'.
6663
Type 'Promise<number>' is not assignable to type 'Promise<string>'.
6764
Type 'number' is not assignable to type 'string'.
@@ -285,7 +282,6 @@ tests/cases/compiler/promisePermutations2.ts(159,21): error TS2345: Argument of
285282
~~~~~~~
286283
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
287284
!!! error TS2453: Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
288-
!!! error TS2453: Type 'string' is not assignable to type 'number'.
289285
var s9g = s9.then(testFunction, nIPromise, sIPromise).then(sIPromise, sIPromise, sIPromise); // ok
290286

291287
var r10 = testFunction10(x => x);
@@ -296,7 +292,6 @@ tests/cases/compiler/promisePermutations2.ts(159,21): error TS2345: Argument of
296292
~~~~~~~~
297293
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
298294
!!! error TS2453: Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
299-
!!! error TS2453: Type 'string' is not assignable to type 'number'.
300295
var r10e = r10.then(testFunction, nIPromise, sIPromise).then(sIPromise, sIPromise, sIPromise); // ok
301296
var s10 = testFunction10P(x => x);
302297
var s10a = s10.then(testFunction10, testFunction10, testFunction10); // ok
@@ -327,7 +322,6 @@ tests/cases/compiler/promisePermutations2.ts(159,21): error TS2345: Argument of
327322
~~~~~~~~~~~~~~
328323
!!! error TS2345: Argument of type '{ (x: number): IPromise<number>; (x: string): IPromise<string>; }' is not assignable to parameter of type '(value: number) => IPromise<string>'.
329324
!!! error TS2345: Type 'IPromise<number>' is not assignable to type 'IPromise<string>'.
330-
!!! error TS2345: Type 'number' is not assignable to type 'string'.
331325
var s11b = s11.then(testFunction11P, testFunction11P, testFunction11P); // ok
332326
~~~~~~~~~~~~~~~
333327
!!! error TS2345: Argument of type '{ (x: number): Promise<number>; (x: string): Promise<string>; }' is not assignable to parameter of type '(value: number) => Promise<string>'.

tests/baselines/reference/promisePermutations3.errors.txt

-6
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ tests/cases/compiler/promisePermutations3.ts(132,19): error TS2345: Argument of
4747
tests/cases/compiler/promisePermutations3.ts(133,19): error TS2345: Argument of type '<T>(x: T, cb: <U>(a: U) => U) => Promise<T>' is not assignable to parameter of type '(value: number) => IPromise<any>'.
4848
tests/cases/compiler/promisePermutations3.ts(136,11): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
4949
Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
50-
Type 'string' is not assignable to type 'number'.
5150
tests/cases/compiler/promisePermutations3.ts(143,12): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
5251
Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
53-
Type 'string' is not assignable to type 'number'.
5452
tests/cases/compiler/promisePermutations3.ts(151,12): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
5553
Type argument candidate 'Promise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
5654
Types of property 'then' are incompatible.
@@ -64,7 +62,6 @@ tests/cases/compiler/promisePermutations3.ts(155,21): error TS2345: Argument of
6462
Type 'number' is not assignable to type 'string'.
6563
tests/cases/compiler/promisePermutations3.ts(157,21): error TS2345: Argument of type '{ (x: number): IPromise<number>; (x: string): IPromise<string>; }' is not assignable to parameter of type '(value: number) => IPromise<string>'.
6664
Type 'IPromise<number>' is not assignable to type 'IPromise<string>'.
67-
Type 'number' is not assignable to type 'string'.
6865
tests/cases/compiler/promisePermutations3.ts(158,21): error TS2345: Argument of type '{ (x: number): Promise<number>; (x: string): Promise<string>; }' is not assignable to parameter of type '(value: number) => Promise<string>'.
6966
Type 'Promise<number>' is not assignable to type 'Promise<string>'.
7067
Type 'number' is not assignable to type 'string'.
@@ -297,7 +294,6 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
297294
~~~~~~~
298295
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
299296
!!! error TS2453: Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
300-
!!! error TS2453: Type 'string' is not assignable to type 'number'.
301297
var s9g = s9.then(testFunction, nIPromise, sIPromise).then(sIPromise, sIPromise, sIPromise); // ok
302298

303299
var r10 = testFunction10(x => x);
@@ -308,7 +304,6 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
308304
~~~~~~~~
309305
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
310306
!!! error TS2453: Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
311-
!!! error TS2453: Type 'string' is not assignable to type 'number'.
312307
var r10e = r10.then(testFunction, nIPromise, sIPromise).then(sIPromise, sIPromise, sIPromise); // ok
313308
var s10 = testFunction10P(x => x);
314309
var s10a = s10.then(testFunction10, testFunction10, testFunction10); // ok
@@ -339,7 +334,6 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
339334
~~~~~~~~~~~~~~
340335
!!! error TS2345: Argument of type '{ (x: number): IPromise<number>; (x: string): IPromise<string>; }' is not assignable to parameter of type '(value: number) => IPromise<string>'.
341336
!!! error TS2345: Type 'IPromise<number>' is not assignable to type 'IPromise<string>'.
342-
!!! error TS2345: Type 'number' is not assignable to type 'string'.
343337
var s11b = s11.then(testFunction11P, testFunction11P, testFunction11P); // error
344338
~~~~~~~~~~~~~~~
345339
!!! error TS2345: Argument of type '{ (x: number): Promise<number>; (x: string): Promise<string>; }' is not assignable to parameter of type '(value: number) => Promise<string>'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
tests/cases/compiler/recursiveTypeRelations.ts(8,5): error TS2391: Function implementation is missing or not immediately following the declaration.
2+
tests/cases/compiler/recursiveTypeRelations.ts(27,38): error TS2304: Cannot find name 'ClassNameObject'.
3+
tests/cases/compiler/recursiveTypeRelations.ts(27,61): error TS2304: Cannot find name 'ClassNameObject'.
4+
5+
6+
==== tests/cases/compiler/recursiveTypeRelations.ts (3 errors) ====
7+
// Repro from #14896
8+
9+
type Attributes<Keys extends string> = {
10+
[Key in Keys]: string;
11+
}
12+
13+
class Query<A extends Attributes<keyof A>> {
14+
multiply<B extends Attributes<keyof B>>(x: B): Query<A & B>;
15+
~~~~~~~~
16+
!!! error TS2391: Function implementation is missing or not immediately following the declaration.
17+
}
18+
19+
// Repro from #14940
20+
21+
type ClassName<S> = keyof S;
22+
type ClassNameMap<S> = { [K in keyof S]?: boolean }
23+
type ClassNameObjectMap<S> = object & ClassNameMap<S>;
24+
type ClassNameArg<S> = ClassName<S> | ClassNameObjectMap<S>;
25+
26+
export function css<S extends { [K in keyof S]: string }>(styles: S, ...classNames: ClassNameArg<S>[]): string {
27+
const args = classNames.map(arg => {
28+
if (arg == null) {
29+
return null;
30+
}
31+
if (typeof arg == "string") {
32+
return styles[arg];
33+
}
34+
if (typeof arg == "object") {
35+
return Object.keys(arg).reduce<ClassNameObject>((obj: ClassNameObject, key: keyof S) => {
36+
~~~~~~~~~~~~~~~
37+
!!! error TS2304: Cannot find name 'ClassNameObject'.
38+
~~~~~~~~~~~~~~~
39+
!!! error TS2304: Cannot find name 'ClassNameObject'.
40+
const exportedClassName = styles[key];
41+
obj[exportedClassName] = (arg as ClassNameMap<S>)[key];
42+
return obj;
43+
}, {});
44+
}
45+
});
46+
return "";
47+
}
48+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//// [recursiveTypeRelations.ts]
2+
// Repro from #14896
3+
4+
type Attributes<Keys extends string> = {
5+
[Key in Keys]: string;
6+
}
7+
8+
class Query<A extends Attributes<keyof A>> {
9+
multiply<B extends Attributes<keyof B>>(x: B): Query<A & B>;
10+
}
11+
12+
// Repro from #14940
13+
14+
type ClassName<S> = keyof S;
15+
type ClassNameMap<S> = { [K in keyof S]?: boolean }
16+
type ClassNameObjectMap<S> = object & ClassNameMap<S>;
17+
type ClassNameArg<S> = ClassName<S> | ClassNameObjectMap<S>;
18+
19+
export function css<S extends { [K in keyof S]: string }>(styles: S, ...classNames: ClassNameArg<S>[]): string {
20+
const args = classNames.map(arg => {
21+
if (arg == null) {
22+
return null;
23+
}
24+
if (typeof arg == "string") {
25+
return styles[arg];
26+
}
27+
if (typeof arg == "object") {
28+
return Object.keys(arg).reduce<ClassNameObject>((obj: ClassNameObject, key: keyof S) => {
29+
const exportedClassName = styles[key];
30+
obj[exportedClassName] = (arg as ClassNameMap<S>)[key];
31+
return obj;
32+
}, {});
33+
}
34+
});
35+
return "";
36+
}
37+
38+
39+
//// [recursiveTypeRelations.js]
40+
"use strict";
41+
// Repro from #14896
42+
exports.__esModule = true;
43+
var Query = (function () {
44+
function Query() {
45+
}
46+
return Query;
47+
}());
48+
function css(styles) {
49+
var classNames = [];
50+
for (var _i = 1; _i < arguments.length; _i++) {
51+
classNames[_i - 1] = arguments[_i];
52+
}
53+
var args = classNames.map(function (arg) {
54+
if (arg == null) {
55+
return null;
56+
}
57+
if (typeof arg == "string") {
58+
return styles[arg];
59+
}
60+
if (typeof arg == "object") {
61+
return Object.keys(arg).reduce(function (obj, key) {
62+
var exportedClassName = styles[key];
63+
obj[exportedClassName] = arg[key];
64+
return obj;
65+
}, {});
66+
}
67+
});
68+
return "";
69+
}
70+
exports.css = css;

tests/baselines/reference/trailingCommaInHeterogenousArrayLiteral1.errors.txt

-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ tests/cases/compiler/trailingCommaInHeterogenousArrayLiteral1.ts(5,19): error TS
22
Type 'string | number' is not assignable to type 'number'.
33
Type 'string' is not assignable to type 'number'.
44
tests/cases/compiler/trailingCommaInHeterogenousArrayLiteral1.ts(6,19): error TS2345: Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'.
5-
Type 'string | number' is not assignable to type 'number'.
6-
Type 'string' is not assignable to type 'number'.
75

86

97
==== tests/cases/compiler/trailingCommaInHeterogenousArrayLiteral1.ts (2 errors) ====
@@ -19,8 +17,6 @@ tests/cases/compiler/trailingCommaInHeterogenousArrayLiteral1.ts(6,19): error TS
1917
this.test([1, 2, "hi", 5]);
2018
~~~~~~~~~~~~~~~
2119
!!! error TS2345: Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'.
22-
!!! error TS2345: Type 'string | number' is not assignable to type 'number'.
23-
!!! error TS2345: Type 'string' is not assignable to type 'number'.
2420
}
2521
}
2622

0 commit comments

Comments
 (0)