Skip to content

Commit 57c9b39

Browse files
fix microsoft#10698: infer literal return type in generic position
1 parent 571d1f7 commit 57c9b39

9 files changed

+51
-6
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4870,7 +4870,8 @@ namespace ts {
48704870
else {
48714871
type = getReturnTypeFromBody(<FunctionLikeDeclaration>signature.declaration);
48724872
}
4873-
if (!popTypeResolution()) {
4873+
// skip warning if type can be inferred, e.g. literal type in contextual typing position
4874+
if (!popTypeResolution() && !(type.flags & TypeFlags.Primitive)) {
48744875
type = anyType;
48754876
if (compilerOptions.noImplicitAny) {
48764877
const declaration = <Declaration>signature.declaration;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [genericCallWithFunctionReturnLiteral.ts]
2+
function foo<T>(t: T) { return t; }
3+
4+
foo(() => 123)
5+
6+
7+
//// [genericCallWithFunctionReturnLiteral.js]
8+
function foo(t) { return t; }
9+
foo(function () { return 123; });
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/genericCallWithFunctionReturnLiteral.ts ===
2+
function foo<T>(t: T) { return t; }
3+
>foo : Symbol(foo, Decl(genericCallWithFunctionReturnLiteral.ts, 0, 0))
4+
>T : Symbol(T, Decl(genericCallWithFunctionReturnLiteral.ts, 0, 13))
5+
>t : Symbol(t, Decl(genericCallWithFunctionReturnLiteral.ts, 0, 16))
6+
>T : Symbol(T, Decl(genericCallWithFunctionReturnLiteral.ts, 0, 13))
7+
>t : Symbol(t, Decl(genericCallWithFunctionReturnLiteral.ts, 0, 16))
8+
9+
foo(() => 123)
10+
>foo : Symbol(foo, Decl(genericCallWithFunctionReturnLiteral.ts, 0, 0))
11+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/genericCallWithFunctionReturnLiteral.ts ===
2+
function foo<T>(t: T) { return t; }
3+
>foo : <T>(t: T) => T
4+
>T : T
5+
>t : T
6+
>T : T
7+
>t : T
8+
9+
foo(() => 123)
10+
>foo(() => 123) : () => number
11+
>foo : <T>(t: T) => T
12+
>() => 123 : () => number
13+
>123 : 123
14+

tests/baselines/reference/implicitAnyFromCircularInference.errors.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ tests/cases/compiler/implicitAnyFromCircularInference.ts(10,5): error TS2502: 'd
55
tests/cases/compiler/implicitAnyFromCircularInference.ts(15,10): error TS7023: 'g' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
66
tests/cases/compiler/implicitAnyFromCircularInference.ts(18,10): error TS7024: Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
77
tests/cases/compiler/implicitAnyFromCircularInference.ts(23,10): error TS7024: Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
8-
tests/cases/compiler/implicitAnyFromCircularInference.ts(26,10): error TS7023: 'h' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
9-
tests/cases/compiler/implicitAnyFromCircularInference.ts(28,14): error TS7023: 'foo' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
10-
tests/cases/compiler/implicitAnyFromCircularInference.ts(41,5): error TS7022: 's' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
11-
tests/cases/compiler/implicitAnyFromCircularInference.ts(46,9): error TS7023: 'x' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
8+
tests/cases/compiler/implicitAnyFromCircularInference.ts(28,10): error TS7023: 'h' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
9+
tests/cases/compiler/implicitAnyFromCircularInference.ts(30,14): error TS7023: 'foo' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
10+
tests/cases/compiler/implicitAnyFromCircularInference.ts(43,5): error TS7022: 's' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
11+
tests/cases/compiler/implicitAnyFromCircularInference.ts(48,9): error TS7023: 'x' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
1212

1313

1414
==== tests/cases/compiler/implicitAnyFromCircularInference.ts (11 errors) ====
@@ -50,6 +50,8 @@ tests/cases/compiler/implicitAnyFromCircularInference.ts(46,9): error TS7023: 'x
5050
~~~~~~~~~~
5151
!!! error TS7024: Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
5252

53+
var f3 = () => f3;
54+
5355
// Error expected
5456
function h() {
5557
~

tests/baselines/reference/implicitAnyFromCircularInference.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var f1 = function () {
2323
// Error expected
2424
var f2 = () => f2();
2525

26+
var f3 = () => f3;
27+
2628
// Error expected
2729
function h() {
2830
return foo();
@@ -67,6 +69,7 @@ var f1 = function () {
6769
};
6870
// Error expected
6971
var f2 = function () { return f2(); };
72+
var f3 = function () { return f3; };
7073
// Error expected
7174
function h() {
7275
return foo();

tests/baselines/reference/recursiveComplicatedClasses.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Symbol {
2424
>bound : boolean
2525

2626
public visible() {
27-
>visible : () => any
27+
>visible : () => boolean
2828

2929
var b: TypeSymbol;
3030
>b : TypeSymbol
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function foo<T>(t: T) { return t; }
2+
3+
foo(() => 123)

tests/cases/compiler/implicitAnyFromCircularInference.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ var f1 = function () {
2424
// Error expected
2525
var f2 = () => f2();
2626

27+
var f3 = () => f3;
28+
2729
// Error expected
2830
function h() {
2931
return foo();

0 commit comments

Comments
 (0)