Repository navigation
TypeScript can't infer type of default parameters #59643
Copy link
Copy link
Open
Labels
Domain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolutionHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Description
🔎 Search Terms
infer default parameters
function wrapper
function factory
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ
- I tried down to version 4.0.3 (since in the earlier version
Generatortype is not generic)
- I tried down to version 4.0.3 (since in the earlier version
⏯ Playground Link
💻 Code
declare function fnDerive<A extends unknown[], R>(
fn: (...args: A) => Generator<unknown, R>,
): unknown /** mocked return */;
fnDerive(function *(a = 0, b = '') {
console.log({
a,
// ^?
b
// ^?
});
});🙁 Actual behavior
The type of a and b is unknown.
🙂 Expected behavior
The type of a and b should be number and string
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 and b types correctly:
declare function fnDerive<A extends unknown[], R>(
fn: (...args: A) => Generator<unknown, R>,
): unknown /** mocked return */;
fnDerive(function *(a?: number | undefined, b?: string | undefined) {
console.log({
a,
// ^?
b
// ^?
});
a ??= 0;
b ??= '';
console.log({
a,
// ^?
b
// ^?
});
});
Metadata
Metadata
Assignees
Labels
Domain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolutionHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Activity
Andarist commentedon Aug 15, 2024
This is not specific to generator functions. It's related to inferring type parameters from unannotated parameters with inferred types from initializers (TS playground):
It's also just related to how there is a contextual signature available here. This touched on what I improved here: #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 😅
[-]TypeScript can't infer type of default parameters in generator functions[/-][+]TypeScript can't infer type of default parameters in factory functions[/+][-]TypeScript can't infer type of default parameters in factory functions[/-][+]TypeScript can't infer type of default parameters[/+]SukkaW commentedon Aug 15, 2024
Updated title and search terms to reflect this~