Skip to content

Commit

Permalink
Merge pull request #14991 from Microsoft/fixTypeRelationStackOverflow
Browse files Browse the repository at this point in the history
Fix type relation stack overflow
  • Loading branch information
ahejlsberg authored Apr 5, 2017
2 parents c62cc3f + 854731a commit 90648fd
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 262 deletions.
317 changes: 163 additions & 154 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(18,1): error T
Types of property 'concat' are incompatible.
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[]; }'.
Type 'A[]' is not assignable to type 'B[]'.
Type 'A' is not assignable to type 'B'.


==== tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts (2 errors) ====
Expand Down Expand Up @@ -42,5 +41,4 @@ tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(18,1): error T
!!! error TS2322: Types of property 'concat' are incompatible.
!!! 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[]; }'.
!!! error TS2322: Type 'A[]' is not assignable to type 'B[]'.
!!! error TS2322: Type 'A' is not assignable to type 'B'.

128 changes: 52 additions & 76 deletions tests/baselines/reference/mappedTypeRelationships.errors.txt

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions tests/baselines/reference/promisePermutations.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ tests/cases/compiler/promisePermutations.ts(133,19): error TS2345: Argument of t
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>'.
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.
Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
Type 'string' is not assignable to type 'number'.
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.
Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
Type 'string' is not assignable to type 'number'.
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.
Type argument candidate 'Promise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
Types of property 'then' are incompatible.
Expand All @@ -61,7 +59,6 @@ tests/cases/compiler/promisePermutations.ts(156,21): error TS2345: Argument of t
Type 'number' is not assignable to type 'string'.
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>'.
Type 'IPromise<number>' is not assignable to type 'IPromise<string>'.
Type 'number' is not assignable to type 'string'.
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>'.
Type 'Promise<number>' is not assignable to type 'Promise<string>'.
Type 'number' is not assignable to type 'string'.
Expand Down Expand Up @@ -286,7 +283,6 @@ tests/cases/compiler/promisePermutations.ts(160,21): error TS2345: Argument of t
~~~~~~~
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
!!! error TS2453: Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
!!! error TS2453: Type 'string' is not assignable to type 'number'.
var s9g = s9.then(testFunction, nIPromise, sIPromise).then(sIPromise, sIPromise, sIPromise); // ok

var r10 = testFunction10(x => x);
Expand All @@ -297,7 +293,6 @@ tests/cases/compiler/promisePermutations.ts(160,21): error TS2345: Argument of t
~~~~~~~~
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
!!! error TS2453: Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
!!! error TS2453: Type 'string' is not assignable to type 'number'.
var r10e = r10.then(testFunction, nIPromise, sIPromise).then(sIPromise, sIPromise, sIPromise); // ok
var s10 = testFunction10P(x => x);
var s10a = s10.then(testFunction10, testFunction10, testFunction10); // ok
Expand Down Expand Up @@ -328,7 +323,6 @@ tests/cases/compiler/promisePermutations.ts(160,21): error TS2345: Argument of t
~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ (x: number): IPromise<number>; (x: string): IPromise<string>; }' is not assignable to parameter of type '(value: number) => IPromise<string>'.
!!! error TS2345: Type 'IPromise<number>' is not assignable to type 'IPromise<string>'.
!!! error TS2345: Type 'number' is not assignable to type 'string'.
var s11b = s11.then(testFunction11P, testFunction11P, testFunction11P); // error
~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ (x: number): Promise<number>; (x: string): Promise<string>; }' is not assignable to parameter of type '(value: number) => Promise<string>'.
Expand Down
6 changes: 0 additions & 6 deletions tests/baselines/reference/promisePermutations2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ tests/cases/compiler/promisePermutations2.ts(132,19): error TS2345: Argument of
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>'.
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.
Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
Type 'string' is not assignable to type 'number'.
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.
Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
Type 'string' is not assignable to type 'number'.
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.
Type argument candidate 'Promise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
Types of property 'then' are incompatible.
Expand All @@ -61,7 +59,6 @@ tests/cases/compiler/promisePermutations2.ts(155,21): error TS2345: Argument of
Type 'number' is not assignable to type 'string'.
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>'.
Type 'IPromise<number>' is not assignable to type 'IPromise<string>'.
Type 'number' is not assignable to type 'string'.
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>'.
Type 'Promise<number>' is not assignable to type 'Promise<string>'.
Type 'number' is not assignable to type 'string'.
Expand Down Expand Up @@ -285,7 +282,6 @@ tests/cases/compiler/promisePermutations2.ts(159,21): error TS2345: Argument of
~~~~~~~
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
!!! error TS2453: Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
!!! error TS2453: Type 'string' is not assignable to type 'number'.
var s9g = s9.then(testFunction, nIPromise, sIPromise).then(sIPromise, sIPromise, sIPromise); // ok

var r10 = testFunction10(x => x);
Expand All @@ -296,7 +292,6 @@ tests/cases/compiler/promisePermutations2.ts(159,21): error TS2345: Argument of
~~~~~~~~
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
!!! error TS2453: Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
!!! error TS2453: Type 'string' is not assignable to type 'number'.
var r10e = r10.then(testFunction, nIPromise, sIPromise).then(sIPromise, sIPromise, sIPromise); // ok
var s10 = testFunction10P(x => x);
var s10a = s10.then(testFunction10, testFunction10, testFunction10); // ok
Expand Down Expand Up @@ -327,7 +322,6 @@ tests/cases/compiler/promisePermutations2.ts(159,21): error TS2345: Argument of
~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ (x: number): IPromise<number>; (x: string): IPromise<string>; }' is not assignable to parameter of type '(value: number) => IPromise<string>'.
!!! error TS2345: Type 'IPromise<number>' is not assignable to type 'IPromise<string>'.
!!! error TS2345: Type 'number' is not assignable to type 'string'.
var s11b = s11.then(testFunction11P, testFunction11P, testFunction11P); // ok
~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ (x: number): Promise<number>; (x: string): Promise<string>; }' is not assignable to parameter of type '(value: number) => Promise<string>'.
Expand Down
6 changes: 0 additions & 6 deletions tests/baselines/reference/promisePermutations3.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ tests/cases/compiler/promisePermutations3.ts(132,19): error TS2345: Argument of
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>'.
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.
Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
Type 'string' is not assignable to type 'number'.
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.
Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
Type 'string' is not assignable to type 'number'.
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.
Type argument candidate 'Promise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
Types of property 'then' are incompatible.
Expand All @@ -64,7 +62,6 @@ tests/cases/compiler/promisePermutations3.ts(155,21): error TS2345: Argument of
Type 'number' is not assignable to type 'string'.
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>'.
Type 'IPromise<number>' is not assignable to type 'IPromise<string>'.
Type 'number' is not assignable to type 'string'.
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>'.
Type 'Promise<number>' is not assignable to type 'Promise<string>'.
Type 'number' is not assignable to type 'string'.
Expand Down Expand Up @@ -297,7 +294,6 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
~~~~~~~
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
!!! error TS2453: Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
!!! error TS2453: Type 'string' is not assignable to type 'number'.
var s9g = s9.then(testFunction, nIPromise, sIPromise).then(sIPromise, sIPromise, sIPromise); // ok

var r10 = testFunction10(x => x);
Expand All @@ -308,7 +304,6 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
~~~~~~~~
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
!!! error TS2453: Type argument candidate 'IPromise<number>' is not a valid type argument because it is not a supertype of candidate 'IPromise<string>'.
!!! error TS2453: Type 'string' is not assignable to type 'number'.
var r10e = r10.then(testFunction, nIPromise, sIPromise).then(sIPromise, sIPromise, sIPromise); // ok
var s10 = testFunction10P(x => x);
var s10a = s10.then(testFunction10, testFunction10, testFunction10); // ok
Expand Down Expand Up @@ -339,7 +334,6 @@ tests/cases/compiler/promisePermutations3.ts(165,21): error TS2345: Argument of
~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ (x: number): IPromise<number>; (x: string): IPromise<string>; }' is not assignable to parameter of type '(value: number) => IPromise<string>'.
!!! error TS2345: Type 'IPromise<number>' is not assignable to type 'IPromise<string>'.
!!! error TS2345: Type 'number' is not assignable to type 'string'.
var s11b = s11.then(testFunction11P, testFunction11P, testFunction11P); // error
~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ (x: number): Promise<number>; (x: string): Promise<string>; }' is not assignable to parameter of type '(value: number) => Promise<string>'.
Expand Down
48 changes: 48 additions & 0 deletions tests/baselines/reference/recursiveTypeRelations.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
tests/cases/compiler/recursiveTypeRelations.ts(8,5): error TS2391: Function implementation is missing or not immediately following the declaration.
tests/cases/compiler/recursiveTypeRelations.ts(27,38): error TS2304: Cannot find name 'ClassNameObject'.
tests/cases/compiler/recursiveTypeRelations.ts(27,61): error TS2304: Cannot find name 'ClassNameObject'.


==== tests/cases/compiler/recursiveTypeRelations.ts (3 errors) ====
// Repro from #14896

type Attributes<Keys extends string> = {
[Key in Keys]: string;
}

class Query<A extends Attributes<keyof A>> {
multiply<B extends Attributes<keyof B>>(x: B): Query<A & B>;
~~~~~~~~
!!! error TS2391: Function implementation is missing or not immediately following the declaration.
}

// Repro from #14940

type ClassName<S> = keyof S;
type ClassNameMap<S> = { [K in keyof S]?: boolean }
type ClassNameObjectMap<S> = object & ClassNameMap<S>;
type ClassNameArg<S> = ClassName<S> | ClassNameObjectMap<S>;

export function css<S extends { [K in keyof S]: string }>(styles: S, ...classNames: ClassNameArg<S>[]): string {
const args = classNames.map(arg => {
if (arg == null) {
return null;
}
if (typeof arg == "string") {
return styles[arg];
}
if (typeof arg == "object") {
return Object.keys(arg).reduce<ClassNameObject>((obj: ClassNameObject, key: keyof S) => {
~~~~~~~~~~~~~~~
!!! error TS2304: Cannot find name 'ClassNameObject'.
~~~~~~~~~~~~~~~
!!! error TS2304: Cannot find name 'ClassNameObject'.
const exportedClassName = styles[key];
obj[exportedClassName] = (arg as ClassNameMap<S>)[key];
return obj;
}, {});
}
});
return "";
}

70 changes: 70 additions & 0 deletions tests/baselines/reference/recursiveTypeRelations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//// [recursiveTypeRelations.ts]
// Repro from #14896

type Attributes<Keys extends string> = {
[Key in Keys]: string;
}

class Query<A extends Attributes<keyof A>> {
multiply<B extends Attributes<keyof B>>(x: B): Query<A & B>;
}

// Repro from #14940

type ClassName<S> = keyof S;
type ClassNameMap<S> = { [K in keyof S]?: boolean }
type ClassNameObjectMap<S> = object & ClassNameMap<S>;
type ClassNameArg<S> = ClassName<S> | ClassNameObjectMap<S>;

export function css<S extends { [K in keyof S]: string }>(styles: S, ...classNames: ClassNameArg<S>[]): string {
const args = classNames.map(arg => {
if (arg == null) {
return null;
}
if (typeof arg == "string") {
return styles[arg];
}
if (typeof arg == "object") {
return Object.keys(arg).reduce<ClassNameObject>((obj: ClassNameObject, key: keyof S) => {
const exportedClassName = styles[key];
obj[exportedClassName] = (arg as ClassNameMap<S>)[key];
return obj;
}, {});
}
});
return "";
}


//// [recursiveTypeRelations.js]
"use strict";
// Repro from #14896
exports.__esModule = true;
var Query = (function () {
function Query() {
}
return Query;
}());
function css(styles) {
var classNames = [];
for (var _i = 1; _i < arguments.length; _i++) {
classNames[_i - 1] = arguments[_i];
}
var args = classNames.map(function (arg) {
if (arg == null) {
return null;
}
if (typeof arg == "string") {
return styles[arg];
}
if (typeof arg == "object") {
return Object.keys(arg).reduce(function (obj, key) {
var exportedClassName = styles[key];
obj[exportedClassName] = arg[key];
return obj;
}, {});
}
});
return "";
}
exports.css = css;
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ tests/cases/compiler/trailingCommaInHeterogenousArrayLiteral1.ts(5,19): error TS
Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/compiler/trailingCommaInHeterogenousArrayLiteral1.ts(6,19): error TS2345: Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'.
Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.


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

Loading

0 comments on commit 90648fd

Please sign in to comment.