Skip to content

Commit dde60bb

Browse files
authored
Merge pull request #16368 from Microsoft/stricterGenericChecks
Stricter generic signature checks
2 parents 7fb821e + 4e9e62d commit dde60bb

File tree

61 files changed

+3515
-441
lines changed

Some content is hidden

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

61 files changed

+3515
-441
lines changed

src/compiler/checker.ts

+58-74
Large diffs are not rendered by default.

src/compiler/commandLineParser.ts

+6
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,12 @@ namespace ts {
620620
category: Diagnostics.Advanced_Options,
621621
description: Diagnostics.The_maximum_dependency_depth_to_search_under_node_modules_and_load_JavaScript_files
622622
},
623+
{
624+
name: "noStrictGenericChecks",
625+
type: "boolean",
626+
category: Diagnostics.Advanced_Options,
627+
description: Diagnostics.Disable_strict_checking_of_generic_signatures_in_function_types,
628+
},
623629
{
624630
// A list of plugins to load in the language service
625631
name: "plugins",

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -3278,6 +3278,10 @@
32783278
"category": "Message",
32793279
"code": 6184
32803280
},
3281+
"Disable strict checking of generic signatures in function types.": {
3282+
"category": "Message",
3283+
"code": 6185
3284+
},
32813285
"Variable '{0}' implicitly has an '{1}' type.": {
32823286
"category": "Error",
32833287
"code": 7005

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3524,6 +3524,7 @@ namespace ts {
35243524
noImplicitAny?: boolean; // Always combine with strict property
35253525
noImplicitReturns?: boolean;
35263526
noImplicitThis?: boolean; // Always combine with strict property
3527+
noStrictGenericChecks?: boolean;
35273528
noUnusedLocals?: boolean;
35283529
noUnusedParameters?: boolean;
35293530
noImplicitUseStrict?: boolean;

tests/baselines/reference/assignmentCompatWithCallSignatures3.errors.txt

+115-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,58 @@
1+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(47,1): error TS2322: Type '(x: number) => number[]' is not assignable to type '<T>(x: T) => T[]'.
2+
Types of parameters 'x' and 'x' are incompatible.
3+
Type 'T' is not assignable to type 'number'.
4+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(50,1): error TS2322: Type '(x: number) => string[]' is not assignable to type '<T>(x: T) => string[]'.
5+
Types of parameters 'x' and 'x' are incompatible.
6+
Type 'T' is not assignable to type 'number'.
7+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(53,1): error TS2322: Type '(x: number) => void' is not assignable to type '<T>(x: T) => T'.
8+
Types of parameters 'x' and 'x' are incompatible.
9+
Type 'T' is not assignable to type 'number'.
10+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(56,1): error TS2322: Type '(x: string, y: number) => string' is not assignable to type '<T, U>(x: T, y: U) => T'.
11+
Types of parameters 'x' and 'x' are incompatible.
12+
Type 'T' is not assignable to type 'string'.
13+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(59,1): error TS2322: Type '(x: (arg: string) => number) => string' is not assignable to type '<T, U>(x: (arg: T) => U) => T'.
14+
Types of parameters 'x' and 'x' are incompatible.
15+
Types of parameters 'arg' and 'arg' are incompatible.
16+
Type 'string' is not assignable to type 'T'.
17+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(62,1): error TS2322: Type '(x: (arg: Base) => Derived) => Base' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U) => T'.
18+
Types of parameters 'x' and 'x' are incompatible.
19+
Types of parameters 'arg' and 'arg' are incompatible.
20+
Type 'Base' is not assignable to type 'T'.
21+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(65,1): error TS2322: Type '(x: (arg: Base) => Derived) => (r: Base) => Derived' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U) => (r: T) => U'.
22+
Types of parameters 'x' and 'x' are incompatible.
23+
Types of parameters 'arg' and 'arg' are incompatible.
24+
Type 'Base' is not assignable to type 'T'.
25+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(68,1): error TS2322: Type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: T) => U) => (r: T) => U'.
26+
Types of parameters 'x' and 'x' are incompatible.
27+
Types of parameters 'arg' and 'arg' are incompatible.
28+
Type 'Base' is not assignable to type 'T'.
129
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(71,1): error TS2322: Type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: string; bing: number; }) => U) => (r: T) => U'.
30+
Types of parameters 'x' and 'x' are incompatible.
31+
Types of parameters 'arg' and 'arg' are incompatible.
32+
Type 'Base' is not assignable to type 'T'.
33+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(74,1): error TS2322: Type '(...x: Derived[]) => Derived' is not assignable to type '<T extends Derived>(...x: T[]) => T'.
34+
Type 'Derived' is not assignable to type 'T'.
35+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(77,1): error TS2322: Type '(x: { foo: string; }, y: { foo: string; bar: string; }) => Base' is not assignable to type '<T extends Base>(x: T, y: T) => T'.
236
Types of parameters 'y' and 'y' are incompatible.
3-
Types of parameters 'arg2' and 'arg2' are incompatible.
4-
Type 'Base' is not assignable to type '{ foo: string; bing: number; }'.
5-
Property 'bing' is missing in type 'Base'.
37+
Type 'T' is not assignable to type '{ foo: string; bar: string; }'.
38+
Type 'Base' is not assignable to type '{ foo: string; bar: string; }'.
39+
Property 'bar' is missing in type 'Base'.
40+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(80,1): error TS2322: Type '(x: Base[], y: Derived2[]) => Derived[]' is not assignable to type '<T extends Base[]>(x: Base[], y: T) => Derived[]'.
41+
Types of parameters 'y' and 'y' are incompatible.
42+
Type 'T' is not assignable to type 'Derived2[]'.
43+
Type 'Base[]' is not assignable to type 'Derived2[]'.
44+
Type 'Base' is not assignable to type 'Derived2'.
45+
Property 'baz' is missing in type 'Base'.
46+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(83,1): error TS2322: Type '(x: Base[], y: Derived[]) => Derived[]' is not assignable to type '<T extends Derived[]>(x: Base[], y: T) => T'.
47+
Type 'Derived[]' is not assignable to type 'T'.
48+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(86,1): error TS2322: Type '(x: { a: string; b: number; }) => Object' is not assignable to type '<T>(x: { a: T; b: T; }) => T'.
49+
Types of parameters 'x' and 'x' are incompatible.
50+
Type '{ a: T; b: T; }' is not assignable to type '{ a: string; b: number; }'.
51+
Types of property 'a' are incompatible.
52+
Type 'T' is not assignable to type 'string'.
653

754

8-
==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts (1 errors) ====
55+
==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts (14 errors) ====
956
// these are all permitted with the current rules, since we do not do contextual signature instantiation
1057

1158
class Base { foo: string; }
@@ -53,51 +100,111 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
53100
var b: <T>(x: T) => T[];
54101
a = b; // ok
55102
b = a; // ok
103+
~
104+
!!! error TS2322: Type '(x: number) => number[]' is not assignable to type '<T>(x: T) => T[]'.
105+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
106+
!!! error TS2322: Type 'T' is not assignable to type 'number'.
56107
var b2: <T>(x: T) => string[];
57108
a2 = b2; // ok
58109
b2 = a2; // ok
110+
~~
111+
!!! error TS2322: Type '(x: number) => string[]' is not assignable to type '<T>(x: T) => string[]'.
112+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
113+
!!! error TS2322: Type 'T' is not assignable to type 'number'.
59114
var b3: <T>(x: T) => T;
60115
a3 = b3; // ok
61116
b3 = a3; // ok
117+
~~
118+
!!! error TS2322: Type '(x: number) => void' is not assignable to type '<T>(x: T) => T'.
119+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
120+
!!! error TS2322: Type 'T' is not assignable to type 'number'.
62121
var b4: <T, U>(x: T, y: U) => T;
63122
a4 = b4; // ok
64123
b4 = a4; // ok
124+
~~
125+
!!! error TS2322: Type '(x: string, y: number) => string' is not assignable to type '<T, U>(x: T, y: U) => T'.
126+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
127+
!!! error TS2322: Type 'T' is not assignable to type 'string'.
65128
var b5: <T, U>(x: (arg: T) => U) => T;
66129
a5 = b5; // ok
67130
b5 = a5; // ok
131+
~~
132+
!!! error TS2322: Type '(x: (arg: string) => number) => string' is not assignable to type '<T, U>(x: (arg: T) => U) => T'.
133+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
134+
!!! error TS2322: Types of parameters 'arg' and 'arg' are incompatible.
135+
!!! error TS2322: Type 'string' is not assignable to type 'T'.
68136
var b6: <T extends Base, U extends Derived>(x: (arg: T) => U) => T;
69137
a6 = b6; // ok
70138
b6 = a6; // ok
139+
~~
140+
!!! error TS2322: Type '(x: (arg: Base) => Derived) => Base' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U) => T'.
141+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
142+
!!! error TS2322: Types of parameters 'arg' and 'arg' are incompatible.
143+
!!! error TS2322: Type 'Base' is not assignable to type 'T'.
71144
var b7: <T extends Base, U extends Derived>(x: (arg: T) => U) => (r: T) => U;
72145
a7 = b7; // ok
73146
b7 = a7; // ok
147+
~~
148+
!!! error TS2322: Type '(x: (arg: Base) => Derived) => (r: Base) => Derived' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U) => (r: T) => U'.
149+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
150+
!!! error TS2322: Types of parameters 'arg' and 'arg' are incompatible.
151+
!!! error TS2322: Type 'Base' is not assignable to type 'T'.
74152
var b8: <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: T) => U) => (r: T) => U;
75153
a8 = b8; // ok
76154
b8 = a8; // ok
155+
~~
156+
!!! error TS2322: Type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: T) => U) => (r: T) => U'.
157+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
158+
!!! error TS2322: Types of parameters 'arg' and 'arg' are incompatible.
159+
!!! error TS2322: Type 'Base' is not assignable to type 'T'.
77160
var b9: <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: string; bing: number }) => U) => (r: T) => U;
78161
a9 = b9; // ok
79162
b9 = a9; // ok
80163
~~
81164
!!! error TS2322: Type '(x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: string; bing: number; }) => U) => (r: T) => U'.
82-
!!! error TS2322: Types of parameters 'y' and 'y' are incompatible.
83-
!!! error TS2322: Types of parameters 'arg2' and 'arg2' are incompatible.
84-
!!! error TS2322: Type 'Base' is not assignable to type '{ foo: string; bing: number; }'.
85-
!!! error TS2322: Property 'bing' is missing in type 'Base'.
165+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
166+
!!! error TS2322: Types of parameters 'arg' and 'arg' are incompatible.
167+
!!! error TS2322: Type 'Base' is not assignable to type 'T'.
86168
var b10: <T extends Derived>(...x: T[]) => T;
87169
a10 = b10; // ok
88170
b10 = a10; // ok
171+
~~~
172+
!!! error TS2322: Type '(...x: Derived[]) => Derived' is not assignable to type '<T extends Derived>(...x: T[]) => T'.
173+
!!! error TS2322: Type 'Derived' is not assignable to type 'T'.
89174
var b11: <T extends Base>(x: T, y: T) => T;
90175
a11 = b11; // ok
91176
b11 = a11; // ok
177+
~~~
178+
!!! error TS2322: Type '(x: { foo: string; }, y: { foo: string; bar: string; }) => Base' is not assignable to type '<T extends Base>(x: T, y: T) => T'.
179+
!!! error TS2322: Types of parameters 'y' and 'y' are incompatible.
180+
!!! error TS2322: Type 'T' is not assignable to type '{ foo: string; bar: string; }'.
181+
!!! error TS2322: Type 'Base' is not assignable to type '{ foo: string; bar: string; }'.
182+
!!! error TS2322: Property 'bar' is missing in type 'Base'.
92183
var b12: <T extends Array<Base>>(x: Array<Base>, y: T) => Array<Derived>;
93184
a12 = b12; // ok
94185
b12 = a12; // ok
186+
~~~
187+
!!! error TS2322: Type '(x: Base[], y: Derived2[]) => Derived[]' is not assignable to type '<T extends Base[]>(x: Base[], y: T) => Derived[]'.
188+
!!! error TS2322: Types of parameters 'y' and 'y' are incompatible.
189+
!!! error TS2322: Type 'T' is not assignable to type 'Derived2[]'.
190+
!!! error TS2322: Type 'Base[]' is not assignable to type 'Derived2[]'.
191+
!!! error TS2322: Type 'Base' is not assignable to type 'Derived2'.
192+
!!! error TS2322: Property 'baz' is missing in type 'Base'.
95193
var b13: <T extends Array<Derived>>(x: Array<Base>, y: T) => T;
96194
a13 = b13; // ok
97195
b13 = a13; // ok
196+
~~~
197+
!!! error TS2322: Type '(x: Base[], y: Derived[]) => Derived[]' is not assignable to type '<T extends Derived[]>(x: Base[], y: T) => T'.
198+
!!! error TS2322: Type 'Derived[]' is not assignable to type 'T'.
98199
var b14: <T>(x: { a: T; b: T }) => T;
99200
a14 = b14; // ok
100201
b14 = a14; // ok
202+
~~~
203+
!!! error TS2322: Type '(x: { a: string; b: number; }) => Object' is not assignable to type '<T>(x: { a: T; b: T; }) => T'.
204+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
205+
!!! error TS2322: Type '{ a: T; b: T; }' is not assignable to type '{ a: string; b: number; }'.
206+
!!! error TS2322: Types of property 'a' are incompatible.
207+
!!! error TS2322: Type 'T' is not assignable to type 'string'.
101208
var b15: <T>(x: T) => T[];
102209
a15 = b15; // ok
103210
b15 = a15; // ok

0 commit comments

Comments
 (0)