Skip to content

Commit 31f0814

Browse files
ahejlsbergmhegazy
authored andcommitted
Simplify generic function call error reporting (#16439)
* Simplify error reporting for generic functions * Accept new baselines * Fix fourslash tests
1 parent 9cd04e0 commit 31f0814

File tree

48 files changed

+1371
-678
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1371
-678
lines changed

src/compiler/checker.ts

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

src/compiler/types.ts

-2
Original file line numberDiff line numberDiff line change
@@ -3406,8 +3406,6 @@ namespace ts {
34063406
signature: Signature; // Generic signature for which inferences are made
34073407
inferences: InferenceInfo[]; // Inferences made for each type parameter
34083408
flags: InferenceFlags; // Inference flags
3409-
failedTypeParameterIndex?: number; // Index of type parameter for which inference failed
3410-
// It is optional because in contextual signature instantiation, nothing fails
34113409
}
34123410

34133411
/* @internal */

tests/baselines/reference/decoratorCallGeneric.errors.txt

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
tests/cases/conformance/decorators/decoratorCallGeneric.ts(7,2): error TS1238: Unable to resolve signature of class decorator when called as an expression.
2-
The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
3-
Type argument candidate 'C' is not a valid type argument because it is not a supertype of candidate 'void'.
1+
tests/cases/conformance/decorators/decoratorCallGeneric.ts(7,2): error TS2345: Argument of type 'typeof C' is not assignable to parameter of type 'I<C>'.
2+
Types of property 'm' are incompatible.
3+
Type '() => void' is not assignable to type '() => C'.
4+
Type 'void' is not assignable to type 'C'.
45

56

67
==== tests/cases/conformance/decorators/decoratorCallGeneric.ts (1 errors) ====
@@ -12,9 +13,10 @@ tests/cases/conformance/decorators/decoratorCallGeneric.ts(7,2): error TS1238: U
1213

1314
@dec
1415
~~~
15-
!!! error TS1238: Unable to resolve signature of class decorator when called as an expression.
16-
!!! error TS1238: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
17-
!!! error TS1238: Type argument candidate 'C' is not a valid type argument because it is not a supertype of candidate 'void'.
16+
!!! error TS2345: Argument of type 'typeof C' is not assignable to parameter of type 'I<C>'.
17+
!!! error TS2345: Types of property 'm' are incompatible.
18+
!!! error TS2345: Type '() => void' is not assignable to type '() => C'.
19+
!!! error TS2345: Type 'void' is not assignable to type 'C'.
1820
class C {
1921
_brand: any;
2022
static m() {}

tests/baselines/reference/defaultBestCommonTypesHaveDecls.errors.txt

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
tests/cases/compiler/defaultBestCommonTypesHaveDecls.ts(2,6): error TS2339: Property 'length' does not exist on type '{}'.
22
tests/cases/compiler/defaultBestCommonTypesHaveDecls.ts(5,6): error TS2339: Property 'length' does not exist on type 'Object'.
3-
tests/cases/compiler/defaultBestCommonTypesHaveDecls.ts(8,14): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
4-
Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate '""'.
3+
tests/cases/compiler/defaultBestCommonTypesHaveDecls.ts(8,24): error TS2345: Argument of type '""' is not assignable to parameter of type '1'.
54

65

76
==== tests/cases/compiler/defaultBestCommonTypesHaveDecls.ts (3 errors) ====
@@ -17,9 +16,8 @@ tests/cases/compiler/defaultBestCommonTypesHaveDecls.ts(8,14): error TS2453: The
1716

1817
function concat<T>(x: T, y: T): T { return null; }
1918
var result = concat(1, ""); // error
20-
~~~~~~
21-
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
22-
!!! error TS2453: Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate '""'.
19+
~~
20+
!!! error TS2345: Argument of type '""' is not assignable to parameter of type '1'.
2321
var elementCount = result.length;
2422

2523
function concat2<T, U>(x: T, y: U) { return null; }
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
tests/cases/compiler/fixTypeParameterInSignatureWithRestParameters.ts(2,1): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
2-
Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate '""'.
1+
tests/cases/compiler/fixTypeParameterInSignatureWithRestParameters.ts(2,8): error TS2345: Argument of type '""' is not assignable to parameter of type 'number'.
32

43

54
==== tests/cases/compiler/fixTypeParameterInSignatureWithRestParameters.ts (1 errors) ====
65
function bar<T>(item1: T, item2: T) { }
76
bar(1, ""); // Should be ok
8-
~~~
9-
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
10-
!!! error TS2453: Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate '""'.
7+
~~
8+
!!! error TS2345: Argument of type '""' is not assignable to parameter of type 'number'.

tests/baselines/reference/for-of39.errors.txt

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1-
tests/cases/conformance/es6/for-ofStatements/for-of39.ts(1,15): error TS2453: The type argument for type parameter 'V' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
2-
Type argument candidate 'boolean' is not a valid type argument because it is not a supertype of candidate 'number'.
1+
tests/cases/conformance/es6/for-ofStatements/for-of39.ts(1,19): error TS2345: Argument of type '([string, true] | [string, 0])[]' is not assignable to parameter of type 'Iterable<[string, boolean]>'.
2+
Types of property '[Symbol.iterator]' are incompatible.
3+
Type '() => IterableIterator<[string, true] | [string, 0]>' is not assignable to type '() => Iterator<[string, boolean]>'.
4+
Type 'IterableIterator<[string, true] | [string, 0]>' is not assignable to type 'Iterator<[string, boolean]>'.
5+
Types of property 'next' are incompatible.
6+
Type '(value?: any) => IteratorResult<[string, true] | [string, 0]>' is not assignable to type '(value?: any) => IteratorResult<[string, boolean]>'.
7+
Type 'IteratorResult<[string, true] | [string, 0]>' is not assignable to type 'IteratorResult<[string, boolean]>'.
8+
Type '[string, true] | [string, 0]' is not assignable to type '[string, boolean]'.
9+
Type '[string, 0]' is not assignable to type '[string, boolean]'.
10+
Type '0' is not assignable to type 'boolean'.
311

412

513
==== tests/cases/conformance/es6/for-ofStatements/for-of39.ts (1 errors) ====
614
var map = new Map([["", true], ["", 0]]);
7-
~~~
8-
!!! error TS2453: The type argument for type parameter 'V' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
9-
!!! error TS2453: Type argument candidate 'boolean' is not a valid type argument because it is not a supertype of candidate 'number'.
15+
~~~~~~~~~~~~~~~~~~~~~
16+
!!! error TS2345: Argument of type '([string, true] | [string, 0])[]' is not assignable to parameter of type 'Iterable<[string, boolean]>'.
17+
!!! error TS2345: Types of property '[Symbol.iterator]' are incompatible.
18+
!!! error TS2345: Type '() => IterableIterator<[string, true] | [string, 0]>' is not assignable to type '() => Iterator<[string, boolean]>'.
19+
!!! error TS2345: Type 'IterableIterator<[string, true] | [string, 0]>' is not assignable to type 'Iterator<[string, boolean]>'.
20+
!!! error TS2345: Types of property 'next' are incompatible.
21+
!!! error TS2345: Type '(value?: any) => IteratorResult<[string, true] | [string, 0]>' is not assignable to type '(value?: any) => IteratorResult<[string, boolean]>'.
22+
!!! error TS2345: Type 'IteratorResult<[string, true] | [string, 0]>' is not assignable to type 'IteratorResult<[string, boolean]>'.
23+
!!! error TS2345: Type '[string, true] | [string, 0]' is not assignable to type '[string, boolean]'.
24+
!!! error TS2345: Type '[string, 0]' is not assignable to type '[string, boolean]'.
25+
!!! error TS2345: Type '0' is not assignable to type 'boolean'.
1026
for (var [k, v] of map) {
1127
k;
1228
v;

tests/baselines/reference/generatorTypeCheck63.errors.txt

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck63.ts(24,61): err
33
Type 'State | 1' is not assignable to type 'StrategicState'.
44
Type '1' has no properties in common with type 'StrategicState'.
55
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck63.ts(29,70): error TS7025: Generator implicitly has type 'IterableIterator<any>' because it does not yield any values. Consider supplying a return type.
6-
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck63.ts(32,42): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
7-
Type argument candidate 'State' is not a valid type argument because it is not a supertype of candidate '1'.
6+
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck63.ts(32,62): error TS2345: Argument of type '(state: State) => IterableIterator<1>' is not assignable to parameter of type '(a: State) => IterableIterator<State>'.
7+
Type 'IterableIterator<1>' is not assignable to type 'IterableIterator<State>'.
8+
Type '1' is not assignable to type 'State'.
89
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck63.ts(36,62): error TS2345: Argument of type '(state: State) => IterableIterator<State | 1>' is not assignable to parameter of type '(a: StrategicState) => IterableIterator<StrategicState>'.
910
Type 'IterableIterator<State | 1>' is not assignable to type 'IterableIterator<StrategicState>'.
1011

@@ -49,9 +50,10 @@ tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck63.ts(36,62): err
4950
});
5051

5152
export const Nothing2: Strategy<State> = strategy("Nothing", function* (state: State) {
52-
~~~~~~~~
53-
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
54-
!!! error TS2453: Type argument candidate 'State' is not a valid type argument because it is not a supertype of candidate '1'.
53+
~~~~~~~~
54+
!!! error TS2345: Argument of type '(state: State) => IterableIterator<1>' is not assignable to parameter of type '(a: State) => IterableIterator<State>'.
55+
!!! error TS2345: Type 'IterableIterator<1>' is not assignable to type 'IterableIterator<State>'.
56+
!!! error TS2345: Type '1' is not assignable to type 'State'.
5557
return 1;
5658
});
5759

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(26,10): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
2-
Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate 'string'.
3-
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(30,15): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
4-
Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate 'T'.
5-
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(33,15): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
6-
Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate 'T'.
7-
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(34,16): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
8-
Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate 'T'.
9-
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(35,15): error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
10-
Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate 'string'.
1+
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(26,18): error TS2345: Argument of type '(a: number) => string' is not assignable to parameter of type '(a: number) => 1'.
2+
Type 'string' is not assignable to type '1'.
3+
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(30,23): error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
4+
Types of parameters 'x' and 'a' are incompatible.
5+
Type '1' is not assignable to type 'T'.
6+
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(33,23): error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
7+
Types of parameters 'x' and 'a' are incompatible.
8+
Type '1' is not assignable to type 'T'.
9+
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(34,24): error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
10+
Types of parameters 'x' and 'a' are incompatible.
11+
Type '1' is not assignable to type 'T'.
12+
tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts(35,23): error TS2345: Argument of type '(a: number) => string' is not assignable to parameter of type '(a: number) => 1'.
13+
Type 'string' is not assignable to type '1'.
1114

1215

1316
==== tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFunctionTypedArguments.ts (5 errors) ====
@@ -37,28 +40,31 @@ tests/cases/conformance/types/typeRelationships/typeInference/genericCallWithFun
3740
var r7 = foo3(1, <Z>(a: Z) => '', ''); // string
3841

3942
var r8 = foo3(1, function (a) { return '' }, 1); // error
40-
~~~~
41-
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
42-
!!! error TS2453: Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate 'string'.
43+
~~~~~~~~
44+
!!! error TS2345: Argument of type '(a: number) => string' is not assignable to parameter of type '(a: number) => 1'.
45+
!!! error TS2345: Type 'string' is not assignable to type '1'.
4346
var r9 = foo3<number, string>(1, (a) => '', ''); // string
4447

4548
function other<T, U>(t: T, u: U) {
4649
var r10 = foo2(1, (x: T) => ''); // error
47-
~~~~
48-
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
49-
!!! error TS2453: Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate 'T'.
50+
~~~~~~~~~~~~
51+
!!! error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
52+
!!! error TS2345: Types of parameters 'x' and 'a' are incompatible.
53+
!!! error TS2345: Type '1' is not assignable to type 'T'.
5054
var r10 = foo2(1, (x) => ''); // string
5155

5256
var r11 = foo3(1, (x: T) => '', ''); // error
53-
~~~~
54-
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
55-
!!! error TS2453: Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate 'T'.
57+
~~~~~~~~~~~~
58+
!!! error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
59+
!!! error TS2345: Types of parameters 'x' and 'a' are incompatible.
60+
!!! error TS2345: Type '1' is not assignable to type 'T'.
5661
var r11b = foo3(1, (x: T) => '', 1); // error
57-
~~~~
58-
!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
59-
!!! error TS2453: Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate 'T'.
62+
~~~~~~~~~~~~
63+
!!! error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
64+
!!! error TS2345: Types of parameters 'x' and 'a' are incompatible.
65+
!!! error TS2345: Type '1' is not assignable to type 'T'.
6066
var r12 = foo3(1, function (a) { return '' }, 1); // error
61-
~~~~
62-
!!! error TS2453: The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
63-
!!! error TS2453: Type argument candidate '1' is not a valid type argument because it is not a supertype of candidate 'string'.
67+
~~~~~~~~
68+
!!! error TS2345: Argument of type '(a: number) => string' is not assignable to parameter of type '(a: number) => 1'.
69+
!!! error TS2345: Type 'string' is not assignable to type '1'.
6470
}

0 commit comments

Comments
 (0)