Skip to content

Incorrect inferred return type when returning object literal from lambda bound to generic parameter #5487

Closed
@russpowers

Description

@russpowers

When passing a function as a generic type T, the compiler correctly infers the return type when passing a previously declared function, but not when the same function is passed as an inline lambda. This seems to only happens with object literals. Classes and builtin return types are inferred properly.

function wrapper<T>(fn: T): T { return fn };

var fn = function(x: number) { return { x } };

// Correct: type of fnWrapper is (x: number) => { x: number }
var fnWrapper = wrapper(fn);

// Incorrect: type of inlineWrapper is (x: number) => any 
var inlineWrapper = wrapper(function(x: number) { return { x } });

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions