Skip to content

Generic inference different between equivalent function expression and shorthand method in object literal #32230

Closed
@minajevs

Description

@minajevs

TypeScript Version: 3.5.1

Search Terms: parameter, inference, order, generic, function expression, function declaration

Code

type Callback<TFoo, TBar> = (foo: TFoo, bar: TBar) => any

declare function example<TFoo, TBar, TCallback extends Callback<TFoo, TBar>>(
    foo: TFoo,
    callback: TCallback,
    bar: TBar,
): TCallback

// bar is infered to be 'string'
example(42, (foo, bar) => ({
    t: () => { }
}), '42')

// bar isn't infered and is 'unknown'
example(42, (foo, bar) => ({
    t() { }
}), '42')

Expected behavior:
foo and bar should be inferred as number and string respectively in both cases

Actual behavior:
When not using arrow function expression bar is not inferred, even though foo is infered in both cases

Playground Link: click

Related Issues: This code worked in TS 3.3.3 , so #31814 is most definitely related, and so probably #30215 is the root cause. #6627 is somewhat related.

Comment: Type inference stops working for types which appear in arguments after inner call to a generic function when function declaration appears in callback return. Using function expressions instead is limiting because those does not have correct this context.

Is this behavior caused by the same limitations as #31814, or is it something what can be improved?

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions