Skip to content

Commit 9c6105d

Browse files
committed
Disable widening for nameless function expressions and arrow functions
1 parent 0477f91 commit 9c6105d

File tree

64 files changed

+496
-419
lines changed

Some content is hidden

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

64 files changed

+496
-419
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18056,7 +18056,7 @@ namespace ts {
1805618056
type = getWidenedLiteralLikeTypeForContextualType(type, contextualType);
1805718057
}
1805818058

18059-
const widenedType = getWidenedType(type);
18059+
const widenedType = (isFunctionExpression(func) && !func.name) || isArrowFunction(func) ? type : getWidenedType(type);
1806018060
switch (functionFlags & FunctionFlags.AsyncGenerator) {
1806118061
case FunctionFlags.AsyncGenerator:
1806218062
return createAsyncIterableIteratorType(widenedType);

tests/baselines/reference/arrayAssignmentTest2.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ tests/cases/compiler/arrayAssignmentTest2.ts(49,1): error TS2322: Type 'I1[]' is
99
Property 'CM3M1' is missing in type 'I1'.
1010
tests/cases/compiler/arrayAssignmentTest2.ts(51,1): error TS2322: Type '() => C1' is not assignable to type 'any[]'.
1111
Property 'push' is missing in type '() => C1'.
12-
tests/cases/compiler/arrayAssignmentTest2.ts(52,1): error TS2322: Type '() => any' is not assignable to type 'any[]'.
13-
Property 'push' is missing in type '() => any'.
12+
tests/cases/compiler/arrayAssignmentTest2.ts(52,1): error TS2322: Type '() => null' is not assignable to type 'any[]'.
13+
Property 'push' is missing in type '() => null'.
1414
tests/cases/compiler/arrayAssignmentTest2.ts(53,1): error TS2322: Type '{ one: number; }' is not assignable to type 'any[]'.
1515
Property 'length' is missing in type '{ one: number; }'.
1616
tests/cases/compiler/arrayAssignmentTest2.ts(55,1): error TS2322: Type 'C1' is not assignable to type 'any[]'.
@@ -92,8 +92,8 @@ tests/cases/compiler/arrayAssignmentTest2.ts(58,1): error TS2322: Type 'I1' is n
9292
!!! error TS2322: Property 'push' is missing in type '() => C1'.
9393
arr_any = function () { return null;} // should be an error - is
9494
~~~~~~~
95-
!!! error TS2322: Type '() => any' is not assignable to type 'any[]'.
96-
!!! error TS2322: Property 'push' is missing in type '() => any'.
95+
!!! error TS2322: Type '() => null' is not assignable to type 'any[]'.
96+
!!! error TS2322: Property 'push' is missing in type '() => null'.
9797
arr_any = o1; // should be an error - is
9898
~~~~~~~
9999
!!! error TS2322: Type '{ one: number; }' is not assignable to type 'any[]'.

tests/baselines/reference/arrayAssignmentTest2.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ arr_any = f1; // should be an error - is
149149
>f1 : () => C1
150150

151151
arr_any = function () { return null;} // should be an error - is
152-
>arr_any = function () { return null;} : () => any
152+
>arr_any = function () { return null;} : () => null
153153
>arr_any : any[]
154-
>function () { return null;} : () => any
154+
>function () { return null;} : () => null
155155
>null : null
156156

157157
arr_any = o1; // should be an error - is

tests/baselines/reference/arrayAssignmentTest4.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/compiler/arrayAssignmentTest4.ts(22,1): error TS2322: Type '() => any' is not assignable to type 'any[]'.
2-
Property 'push' is missing in type '() => any'.
1+
tests/cases/compiler/arrayAssignmentTest4.ts(22,1): error TS2322: Type '() => null' is not assignable to type 'any[]'.
2+
Property 'push' is missing in type '() => null'.
33
tests/cases/compiler/arrayAssignmentTest4.ts(23,1): error TS2322: Type 'C3' is not assignable to type 'any[]'.
44
Property 'length' is missing in type 'C3'.
55

@@ -28,8 +28,8 @@ tests/cases/compiler/arrayAssignmentTest4.ts(23,1): error TS2322: Type 'C3' is n
2828

2929
arr_any = function () { return null;} // should be an error - is
3030
~~~~~~~
31-
!!! error TS2322: Type '() => any' is not assignable to type 'any[]'.
32-
!!! error TS2322: Property 'push' is missing in type '() => any'.
31+
!!! error TS2322: Type '() => null' is not assignable to type 'any[]'.
32+
!!! error TS2322: Property 'push' is missing in type '() => null'.
3333
arr_any = c3; // should be an error - is
3434
~~~~~~~
3535
!!! error TS2322: Type 'C3' is not assignable to type 'any[]'.

tests/baselines/reference/arrayAssignmentTest4.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ var arr_any: any[] = [];
3636
>[] : undefined[]
3737

3838
arr_any = function () { return null;} // should be an error - is
39-
>arr_any = function () { return null;} : () => any
39+
>arr_any = function () { return null;} : () => null
4040
>arr_any : any[]
41-
>function () { return null;} : () => any
41+
>function () { return null;} : () => null
4242
>null : null
4343

4444
arr_any = c3; // should be an error - is

tests/baselines/reference/arrowFunctionContexts.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ window.setTimeout(() => null, 100);
3636
>window.setTimeout : any
3737
>window : any
3838
>setTimeout : any
39-
>() => null : () => any
39+
>() => null : () => null
4040
>null : null
4141
>100 : 100
4242

@@ -139,7 +139,7 @@ module M2 {
139139
>window.setTimeout : any
140140
>window : any
141141
>setTimeout : any
142-
>() => null : () => any
142+
>() => null : () => null
143143
>null : null
144144
>100 : 100
145145

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/arrowFunctionMakesStrictLiteralCheck.ts(6,17): error TS2322: Type '() => { x: number; y: number; }' is not assignable to type 'XGetter'.
2+
Type '{ x: number; y: number; }' is not assignable to type 'X'.
3+
Object literal may only specify known properties, and 'y' does not exist in type 'X'.
4+
5+
6+
==== tests/cases/compiler/arrowFunctionMakesStrictLiteralCheck.ts (1 errors) ====
7+
interface X { x: number; }
8+
9+
type XGetter = () => X;
10+
11+
const getX2: XGetter = () => {
12+
return { x: 1, y: 2 }; // Expect excess property error on `y`
13+
~~~~
14+
!!! error TS2322: Type '() => { x: number; y: number; }' is not assignable to type 'XGetter'.
15+
!!! error TS2322: Type '{ x: number; y: number; }' is not assignable to type 'X'.
16+
!!! error TS2322: Object literal may only specify known properties, and 'y' does not exist in type 'X'.
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [arrowFunctionMakesStrictLiteralCheck.ts]
2+
interface X { x: number; }
3+
4+
type XGetter = () => X;
5+
6+
const getX2: XGetter = () => {
7+
return { x: 1, y: 2 }; // Expect excess property error on `y`
8+
}
9+
10+
//// [arrowFunctionMakesStrictLiteralCheck.js]
11+
var getX2 = function () {
12+
return { x: 1, y: 2 }; // Expect excess property error on `y`
13+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/arrowFunctionMakesStrictLiteralCheck.ts ===
2+
interface X { x: number; }
3+
>X : Symbol(X, Decl(arrowFunctionMakesStrictLiteralCheck.ts, 0, 0))
4+
>x : Symbol(X.x, Decl(arrowFunctionMakesStrictLiteralCheck.ts, 0, 13))
5+
6+
type XGetter = () => X;
7+
>XGetter : Symbol(XGetter, Decl(arrowFunctionMakesStrictLiteralCheck.ts, 0, 26))
8+
>X : Symbol(X, Decl(arrowFunctionMakesStrictLiteralCheck.ts, 0, 0))
9+
10+
const getX2: XGetter = () => {
11+
>getX2 : Symbol(getX2, Decl(arrowFunctionMakesStrictLiteralCheck.ts, 4, 5))
12+
>XGetter : Symbol(XGetter, Decl(arrowFunctionMakesStrictLiteralCheck.ts, 0, 26))
13+
14+
return { x: 1, y: 2 }; // Expect excess property error on `y`
15+
>x : Symbol(x, Decl(arrowFunctionMakesStrictLiteralCheck.ts, 5, 9))
16+
>y : Symbol(y, Decl(arrowFunctionMakesStrictLiteralCheck.ts, 5, 15))
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/arrowFunctionMakesStrictLiteralCheck.ts ===
2+
interface X { x: number; }
3+
>X : X
4+
>x : number
5+
6+
type XGetter = () => X;
7+
>XGetter : XGetter
8+
>X : X
9+
10+
const getX2: XGetter = () => {
11+
>getX2 : XGetter
12+
>XGetter : XGetter
13+
>() => { return { x: 1, y: 2 }; // Expect excess property error on `y`} : () => { x: number; y: number; }
14+
15+
return { x: 1, y: 2 }; // Expect excess property error on `y`
16+
>{ x: 1, y: 2 } : { x: number; y: number; }
17+
>x : number
18+
>1 : 1
19+
>y : number
20+
>2 : 2
21+
}

tests/baselines/reference/assignmentCompatBug2.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ tests/cases/compiler/assignmentCompatBug2.ts(15,1): error TS2322: Type '{ f: (n:
88
Property 'm' is missing in type '{ f: (n: number) => number; g: (s: string) => number; }'.
99
tests/cases/compiler/assignmentCompatBug2.ts(20,1): error TS2322: Type '{ f: (n: number) => number; m: number; }' is not assignable to type '{ f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }'.
1010
Property 'g' is missing in type '{ f: (n: number) => number; m: number; }'.
11-
tests/cases/compiler/assignmentCompatBug2.ts(33,1): error TS2322: Type '{ f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => any; }' is not assignable to type '{ f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }'.
12-
Property 'm' is missing in type '{ f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => any; }'.
11+
tests/cases/compiler/assignmentCompatBug2.ts(33,1): error TS2322: Type '{ f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => null; }' is not assignable to type '{ f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }'.
12+
Property 'm' is missing in type '{ f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => null; }'.
1313

1414

1515
==== tests/cases/compiler/assignmentCompatBug2.ts (6 errors) ====
@@ -62,8 +62,8 @@ tests/cases/compiler/assignmentCompatBug2.ts(33,1): error TS2322: Type '{ f: (n:
6262

6363
b3 = {
6464
~~
65-
!!! error TS2322: Type '{ f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => any; }' is not assignable to type '{ f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }'.
66-
!!! error TS2322: Property 'm' is missing in type '{ f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => any; }'.
65+
!!! error TS2322: Type '{ f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => null; }' is not assignable to type '{ f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }'.
66+
!!! error TS2322: Property 'm' is missing in type '{ f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => null; }'.
6767
f: (n) => { return 0; },
6868
g: (s) => { return 0; },
6969
n: 0,

tests/baselines/reference/assignmentCompatBug2.types

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ b3 = {
9393
}; // error
9494

9595
b3 = {
96-
>b3 = { f: (n) => { return 0; }, g: (s) => { return 0; }, m: 0, n: 0, k: (a) =>{ return null; },} : { f: (n: number) => number; g: (s: string) => number; m: number; n: number; k: (a: any) => any; }
96+
>b3 = { f: (n) => { return 0; }, g: (s) => { return 0; }, m: 0, n: 0, k: (a) =>{ return null; },} : { f: (n: number) => number; g: (s: string) => number; m: number; n: number; k: (a: any) => null; }
9797
>b3 : { f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }
98-
>{ f: (n) => { return 0; }, g: (s) => { return 0; }, m: 0, n: 0, k: (a) =>{ return null; },} : { f: (n: number) => number; g: (s: string) => number; m: number; n: number; k: (a: any) => any; }
98+
>{ f: (n) => { return 0; }, g: (s) => { return 0; }, m: 0, n: 0, k: (a) =>{ return null; },} : { f: (n: number) => number; g: (s: string) => number; m: number; n: number; k: (a: any) => null; }
9999

100100
f: (n) => { return 0; },
101101
>f : (n: number) => number
@@ -118,17 +118,17 @@ b3 = {
118118
>0 : 0
119119

120120
k: (a) =>{ return null; },
121-
>k : (a: any) => any
122-
>(a) =>{ return null; } : (a: any) => any
121+
>k : (a: any) => null
122+
>(a) =>{ return null; } : (a: any) => null
123123
>a : any
124124
>null : null
125125

126126
}; // ok
127127

128128
b3 = {
129-
>b3 = { f: (n) => { return 0; }, g: (s) => { return 0; }, n: 0, k: (a) =>{ return null; },} : { f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => any; }
129+
>b3 = { f: (n) => { return 0; }, g: (s) => { return 0; }, n: 0, k: (a) =>{ return null; },} : { f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => null; }
130130
>b3 : { f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }
131-
>{ f: (n) => { return 0; }, g: (s) => { return 0; }, n: 0, k: (a) =>{ return null; },} : { f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => any; }
131+
>{ f: (n) => { return 0; }, g: (s) => { return 0; }, n: 0, k: (a) =>{ return null; },} : { f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => null; }
132132

133133
f: (n) => { return 0; },
134134
>f : (n: number) => number
@@ -147,8 +147,8 @@ b3 = {
147147
>0 : 0
148148

149149
k: (a) =>{ return null; },
150-
>k : (a: any) => any
151-
>(a) =>{ return null; } : (a: any) => any
150+
>k : (a: any) => null
151+
>(a) =>{ return null; } : (a: any) => null
152152
>a : any
153153
>null : null
154154

tests/baselines/reference/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.errors.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.ts(14,13): error TS2322: Type '(x: T) => any' is not assignable to type '() => T'.
2-
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.ts(23,13): error TS2322: Type '(x: T, y: T) => any' is not assignable to type '(x: T) => T'.
1+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.ts(14,13): error TS2322: Type '(x: T) => null' is not assignable to type '() => T'.
2+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.ts(23,13): error TS2322: Type '(x: T, y: T) => null' is not assignable to type '(x: T) => T'.
33
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.ts(63,9): error TS2322: Type '() => T' is not assignable to type '<T>() => T'.
44
Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
55
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.ts(64,9): error TS2322: Type '(x?: T) => T' is not assignable to type '<T>() => T'.
@@ -64,8 +64,8 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
6464
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.ts(91,9): error TS2322: Type '(x?: T, y?: T) => T' is not assignable to type '<T>(x?: T, y?: T) => T'.
6565
Types of parameters 'x' and 'x' are incompatible.
6666
Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
67-
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.ts(107,13): error TS2322: Type '<T>(x: T) => any' is not assignable to type '<T>() => T'.
68-
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.ts(116,13): error TS2322: Type '<T>(x: T, y: T) => any' is not assignable to type '<T>(x: T) => T'.
67+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.ts(107,13): error TS2322: Type '<T>(x: T) => null' is not assignable to type '<T>() => T'.
68+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.ts(116,13): error TS2322: Type '<T>(x: T, y: T) => null' is not assignable to type '<T>(x: T) => T'.
6969

7070

7171
==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithGenericCallSignaturesWithOptionalParameters.ts (29 errors) ====
@@ -84,7 +84,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
8484
this.a = (x?: T) => null; // ok, same T of required params
8585
this.a = (x: T) => null; // error, too many required params
8686
~~~~~~
87-
!!! error TS2322: Type '(x: T) => any' is not assignable to type '() => T'.
87+
!!! error TS2322: Type '(x: T) => null' is not assignable to type '() => T'.
8888

8989
this.a2 = () => null; // ok, same T of required params
9090
this.a2 = (x?: T) => null; // ok, same T of required params
@@ -95,7 +95,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
9595
this.a3 = (x: T) => null; // ok, same T of required params
9696
this.a3 = (x: T, y: T) => null; // error, too many required params
9797
~~~~~~~
98-
!!! error TS2322: Type '(x: T, y: T) => any' is not assignable to type '(x: T) => T'.
98+
!!! error TS2322: Type '(x: T, y: T) => null' is not assignable to type '(x: T) => T'.
9999

100100
this.a4 = () => null; // ok, fewer required params
101101
this.a4 = (x?: T, y?: T) => null; // ok, fewer required params
@@ -270,7 +270,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
270270
this.a = <T>(x?: T) => null; // ok, same T of required params
271271
this.a = <T>(x: T) => null; // error, too many required params
272272
~~~~~~
273-
!!! error TS2322: Type '<T>(x: T) => any' is not assignable to type '<T>() => T'.
273+
!!! error TS2322: Type '<T>(x: T) => null' is not assignable to type '<T>() => T'.
274274

275275
this.a2 = <T>() => null; // ok, same T of required params
276276
this.a2 = <T>(x?: T) => null; // ok, same T of required params
@@ -281,7 +281,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
281281
this.a3 = <T>(x: T) => null; // ok, same T of required params
282282
this.a3 = <T>(x: T, y: T) => null; // error, too many required params
283283
~~~~~~~
284-
!!! error TS2322: Type '<T>(x: T, y: T) => any' is not assignable to type '<T>(x: T) => T'.
284+
!!! error TS2322: Type '<T>(x: T, y: T) => null' is not assignable to type '<T>(x: T) => T'.
285285

286286
this.a4 = <T>() => null; // ok, fewer required params
287287
this.a4 = <T>(x?: T, y?: T) => null; // ok, fewer required params

0 commit comments

Comments
 (0)