Open SukkaW opened 2 months ago
This is not specific to generator functions. It's related to inferring type parameters from unannotated parameters with inferred types from initializers (TS playground):
declare function fnDerive<A extends unknown[]>(fn: (...args: A) => void): A;
const result = fnDerive(function (a = 0, b = "") {});
// ^? const result: [a?: unknown, b?: unknown]
declare function fnDerive2<A, B>(fn: (a: A, b: B) => void): [A, B];
const result2 = fnDerive2(function (a = 0, b = "") {});
// ^? const result2: [unknown, unknown]
It's also just related to how there is a contextual signature available here. This touched on what I improved here: https://github.com/microsoft/TypeScript/pull/56506 . This PR didn't break it anyhow when it comes to the case presented here but it also didn't improve it.
So the question here is also - how both situations should be differentiated? There are situations in which the contextual parameter type should be preferred. Perhaps it could be done based on checkMode & CheckMode.Inferential
, or maybe based on the uninstantiated contextual signature when the parameter's type is a type variable.
I'd love to experiment with this but I have a long list of things to experiment with so no promises π
This is not specific to generator functions.
Updated title and search terms to reflect this~
π Search Terms
infer default parameters function wrapper function factory
π Version & Regression Information
Generator
type is not generic)β― Playground Link
https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtVNQBEQYsA3EAHgEF4QAPDEVYAZ3jQGtUcB3VANoBdADTwASgD4AFACh4CggC540gHQbYAc1YrqASngBeSfADizElAw4YlLj35ipI2fpUO++APQAqX-AAtjhgnCDA8HAYyDD4vt4A3LKyiEQk5CDSKOjYePC+0lDG8AAMYgBGxQDkVYYA3vLwYHisOBAgahA4WtINivBQrvDe3vAAegD8jQrl8iPjUwoAvvpJK0lAA
π» Code
π Actual behavior
The type of
a
andb
isunknown
.π Expected behavior
The type of
a
andb
should benumber
andstring
Additional information about the issue
I was working with a library
gensync
when I hit this issue, and I extracted the minimum reproduction out of it.Note that if I don't use default parameters, typescript can infer the
a
andb
types correctly: