Skip to content

Commit 27b0c39

Browse files
committed
Only disable widening when the expression is contextually typed
1 parent c38f3a2 commit 27b0c39

19 files changed

+145
-80
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18251,7 +18251,7 @@ namespace ts {
1825118251
type = getWidenedLiteralLikeTypeForContextualType(type, contextualType);
1825218252
}
1825318253

18254-
const widenedType = (isFunctionExpression(func) && !func.name) || isArrowFunction(func) ? type : getWidenedType(type);
18254+
const widenedType = ((isFunctionExpression(func) && !func.name) || isArrowFunction(func)) && getContextualType(func) ? type : getWidenedType(type);
1825518255
switch (functionFlags & FunctionFlags.AsyncGenerator) {
1825618256
case FunctionFlags.AsyncGenerator:
1825718257
return createAsyncIterableIteratorType(widenedType);

tests/baselines/reference/callNonGenericFunctionWithTypeArguments.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ var r = f<string>(1);
1414
>1 : 1
1515

1616
var f2 = (x: number) => { return null; }
17-
>f2 : (x: number) => null
18-
>(x: number) => { return null; } : (x: number) => null
17+
>f2 : (x: number) => any
18+
>(x: number) => { return null; } : (x: number) => any
1919
>x : number
2020
>null : null
2121

2222
var r2 = f2<string>(1);
2323
>r2 : any
24-
>f2<string>(1) : null
25-
>f2 : (x: number) => null
24+
>f2<string>(1) : any
25+
>f2 : (x: number) => any
2626
>1 : 1
2727

2828
var f3: { (x: number): any; }

tests/baselines/reference/declarationEmitAliasExportStar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ export interface ThingB {
2828
export * from "./thingB";
2929
//// [index.d.ts]
3030
import * as things from "./things";
31-
export declare const thing2: (param: things.ThingB) => null;
31+
export declare const thing2: (param: things.ThingB) => any;

tests/baselines/reference/declarationEmitAliasExportStar.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import * as things from "./things";
99
>things : typeof things
1010

1111
export const thing2 = (param: things.ThingB) => null;
12-
>thing2 : (param: things.ThingB) => null
13-
>(param: things.ThingB) => null : (param: things.ThingB) => null
12+
>thing2 : (param: things.ThingB) => any
13+
>(param: things.ThingB) => null : (param: things.ThingB) => any
1414
>param : things.ThingB
1515
>things : any
1616
>ThingB : things.ThingB
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
=== tests/cases/compiler/decoratorMetadataNoStrictNull.ts ===
22
const dec = (obj: {}, prop: string) => undefined
3-
>dec : (obj: {}, prop: string) => undefined
4-
>(obj: {}, prop: string) => undefined : (obj: {}, prop: string) => undefined
3+
>dec : (obj: {}, prop: string) => any
4+
>(obj: {}, prop: string) => undefined : (obj: {}, prop: string) => any
55
>obj : {}
66
>prop : string
77
>undefined : undefined
@@ -10,11 +10,11 @@ class Foo {
1010
>Foo : Foo
1111

1212
@dec public foo: string | null;
13-
>dec : (obj: {}, prop: string) => undefined
13+
>dec : (obj: {}, prop: string) => any
1414
>foo : string
1515
>null : null
1616

1717
@dec public bar: string;
18-
>dec : (obj: {}, prop: string) => undefined
18+
>dec : (obj: {}, prop: string) => any
1919
>bar : string
2020
}

tests/baselines/reference/fatarrowfunctionsErrors.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ var y = x:number => x*x;
3939
>x : any
4040

4141
false? (() => null): null;
42-
>false? (() => null): null : () => null
42+
>false? (() => null): null : () => any
4343
>false : false
44-
>(() => null) : () => null
45-
>() => null : () => null
44+
>(() => null) : () => any
45+
>() => null : () => any
4646
>null : null
4747
>null : null
4848

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [functionExpressionReturnTypeNotFresh.ts]
2+
const a = () => ({
3+
a: 12,
4+
b: 11
5+
});
6+
7+
declare function f(arg: {a: number}): void;
8+
f(a());
9+
10+
//// [functionExpressionReturnTypeNotFresh.js]
11+
"use strict";
12+
var a = function () { return ({
13+
a: 12,
14+
b: 11
15+
}); };
16+
f(a());
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/functionExpressionReturnTypeNotFresh.ts ===
2+
const a = () => ({
3+
>a : Symbol(a, Decl(functionExpressionReturnTypeNotFresh.ts, 0, 5))
4+
5+
a: 12,
6+
>a : Symbol(a, Decl(functionExpressionReturnTypeNotFresh.ts, 0, 18))
7+
8+
b: 11
9+
>b : Symbol(b, Decl(functionExpressionReturnTypeNotFresh.ts, 1, 10))
10+
11+
});
12+
13+
declare function f(arg: {a: number}): void;
14+
>f : Symbol(f, Decl(functionExpressionReturnTypeNotFresh.ts, 3, 3))
15+
>arg : Symbol(arg, Decl(functionExpressionReturnTypeNotFresh.ts, 5, 19))
16+
>a : Symbol(a, Decl(functionExpressionReturnTypeNotFresh.ts, 5, 25))
17+
18+
f(a());
19+
>f : Symbol(f, Decl(functionExpressionReturnTypeNotFresh.ts, 3, 3))
20+
>a : Symbol(a, Decl(functionExpressionReturnTypeNotFresh.ts, 0, 5))
21+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/functionExpressionReturnTypeNotFresh.ts ===
2+
const a = () => ({
3+
>a : () => { a: number; b: number; }
4+
>() => ({ a: 12, b: 11}) : () => { a: number; b: number; }
5+
>({ a: 12, b: 11}) : { a: number; b: number; }
6+
>{ a: 12, b: 11} : { a: number; b: number; }
7+
8+
a: 12,
9+
>a : number
10+
>12 : 12
11+
12+
b: 11
13+
>b : number
14+
>11 : 11
15+
16+
});
17+
18+
declare function f(arg: {a: number}): void;
19+
>f : (arg: { a: number; }) => void
20+
>arg : { a: number; }
21+
>a : number
22+
23+
f(a());
24+
>f(a()) : void
25+
>f : (arg: { a: number; }) => void
26+
>a() : { a: number; b: number; }
27+
>a : () => { a: number; b: number; }
28+

tests/baselines/reference/functionImplementations.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ var nu = null;
112112

113113
var nu = function () {
114114
>nu : any
115-
>function () { return null;} () : null
116-
>function () { return null;} : () => null
115+
>function () { return null;} () : any
116+
>function () { return null;} : () => any
117117

118118
return null;
119119
>null : null
@@ -127,8 +127,8 @@ var un = undefined;
127127

128128
var un = function () {
129129
>un : any
130-
>function () { return undefined;} () : undefined
131-
>function () { return undefined;} : () => undefined
130+
>function () { return undefined;} () : any
131+
>function () { return undefined;} : () => any
132132

133133
return undefined;
134134
>undefined : undefined

0 commit comments

Comments
 (0)