A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/microsoft/TypeScript/issues/32230 below:

Generic inference different between equivalent function expression and shorthand method in object literal · Issue #32230 · microsoft/TypeScript · GitHub

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?


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4