Skip to content

fix #10698: infer primitive return type in generic position #11313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4870,7 +4870,8 @@ namespace ts {
else {
type = getReturnTypeFromBody(<FunctionLikeDeclaration>signature.declaration);
}
if (!popTypeResolution()) {
// skip warning if type can be inferred, e.g. literal type in contextual typing position
if (!popTypeResolution() && !(type.flags & TypeFlags.Primitive)) {
type = anyType;
if (compilerOptions.noImplicitAny) {
const declaration = <Declaration>signature.declaration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [genericCallWithFunctionReturnLiteral.ts]
function foo<T>(t: T) { return t; }

foo(() => 123)


//// [genericCallWithFunctionReturnLiteral.js]
function foo(t) { return t; }
foo(function () { return 123; });
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/genericCallWithFunctionReturnLiteral.ts ===
function foo<T>(t: T) { return t; }
>foo : Symbol(foo, Decl(genericCallWithFunctionReturnLiteral.ts, 0, 0))
>T : Symbol(T, Decl(genericCallWithFunctionReturnLiteral.ts, 0, 13))
>t : Symbol(t, Decl(genericCallWithFunctionReturnLiteral.ts, 0, 16))
>T : Symbol(T, Decl(genericCallWithFunctionReturnLiteral.ts, 0, 13))
>t : Symbol(t, Decl(genericCallWithFunctionReturnLiteral.ts, 0, 16))

foo(() => 123)
>foo : Symbol(foo, Decl(genericCallWithFunctionReturnLiteral.ts, 0, 0))

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/genericCallWithFunctionReturnLiteral.ts ===
function foo<T>(t: T) { return t; }
>foo : <T>(t: T) => T
>T : T
>t : T
>T : T
>t : T

foo(() => 123)
>foo(() => 123) : () => number
>foo : <T>(t: T) => T
>() => 123 : () => number
>123 : 123

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ tests/cases/compiler/implicitAnyFromCircularInference.ts(10,5): error TS2502: 'd
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.


==== tests/cases/compiler/implicitAnyFromCircularInference.ts (11 errors) ====
Expand Down Expand Up @@ -50,6 +50,8 @@ tests/cases/compiler/implicitAnyFromCircularInference.ts(46,9): error TS7023: 'x
~~~~~~~~~~
!!! 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.

var f3 = () => f3;

// Error expected
function h() {
~
Expand Down
3 changes: 3 additions & 0 deletions tests/baselines/reference/implicitAnyFromCircularInference.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var f1 = function () {
// Error expected
var f2 = () => f2();

var f3 = () => f3;

// Error expected
function h() {
return foo();
Expand Down Expand Up @@ -67,6 +69,7 @@ var f1 = function () {
};
// Error expected
var f2 = function () { return f2(); };
var f3 = function () { return f3; };
// Error expected
function h() {
return foo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Symbol {
>bound : boolean

public visible() {
>visible : () => any
>visible : () => boolean

var b: TypeSymbol;
>b : TypeSymbol
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/compiler/genericCallWithFunctionReturnLiteral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function foo<T>(t: T) { return t; }

foo(() => 123)
2 changes: 2 additions & 0 deletions tests/cases/compiler/implicitAnyFromCircularInference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var f1 = function () {
// Error expected
var f2 = () => f2();

var f3 = () => f3;

// Error expected
function h() {
return foo();
Expand Down