π Search Terms
function parameter inference
π Version & Regression Information
- This changed between versions 3.7.5 and 3.8.3
β― Playground Link
https://www.typescriptlang.org/play/?#code/C4TwDgpgBA8mwEsD2A7AzgHgCoGFXAgA9gA+KAXigG8BYAKCilQGVgBDAJ2AH4AuKABQBKCmVz4iweo1QBRFABM+ggMYTi-cSgLER5MgDckCBfQC+9egDMArihWJUUFRwhsC2PNskkBAIxsEABsFCA5+AU4Ac34-JCQgtxQ9MjhHdE91UhFaBihXYBsOFCgA4NCOAWAOGwghc0s6AHomqAB1JA4AazR6FzcCAUiOGNL4xLZk0UFcmRRWTmAIlJmoRABbCH4UCAB3KAARdwhhKDMhABppJhR5BQi1b11p2cZnVDQEiAA6IKQogSPHTAb4bOrXMxXOjnep0egtdqdHp9VzHIYrASvFjsLjLaaYtYITbbPaHNEic5QuZ3B5ZFavRiPT6JX7-QFZUFE8F5SHmISw+GtABibGCaAAhCiBidhlEMVj5jiloJ5YTiVAdvsjoMKZdrnJFLSnsB6ddGR8vqyAUDJJzNiIEbIOBxOvwAOQ24huqAINBMKxrcDQN12LooJC7FBuiFQmH0IA
π» Code
type Options<TContext> = {
onStart?: () => TContext
onEnd?: (context: TContext) => void
}
function create<TContext>(builder: (arg: boolean) => Options<TContext>) {
return builder(true)
}
// Works
create((arg: boolean) => ({
onStart: () => ({ time: new Date() }),
onEnd: (context) => console.log(context.time)
}))
// Works
create(() => ({
onStart: () => ({ time: new Date() }),
onEnd: (context) => console.log(context.time)
}))
// Fails!
create((arg) => ({
onStart: () => ({ time: new Date() }),
onEnd: (context) => console.log(context.time) // Error: 'context' is of type 'unknown'
}))
π Actual behavior
The value of TContext is not inferred properly when an parameter is included in the builder function. Removing the argument resolves the issue, as does adding an explicit type for the argument.
π Expected behavior
The value of TContext should be inferred properly, regardless of if a parameter is included in the builder function, or if the parameter has or does not have an explicit type.
Additional information about the issue
No response
π Search Terms
function parameter inference
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?#code/C4TwDgpgBA8mwEsD2A7AzgHgCoGFXAgA9gA+KAXigG8BYAKCilQGVgBDAJ2AH4AuKABQBKCmVz4iweo1QBRFABM+ggMYTi-cSgLER5MgDckCBfQC+9egDMArihWJUUFRwhsC2PNskkBAIxsEABsFCA5+AU4Ac34-JCQgtxQ9MjhHdE91UhFaBihXYBsOFCgA4NCOAWAOGwghc0s6AHomqAB1JA4AazR6FzcCAUiOGNL4xLZk0UFcmRRWTmAIlJmoRABbCH4UCAB3KAARdwhhKDMhABppJhR5BQi1b11p2cZnVDQEiAA6IKQogSPHTAb4bOrXMxXOjnep0egtdqdHp9VzHIYrASvFjsLjLaaYtYITbbPaHNEic5QuZ3B5ZFavRiPT6JX7-QFZUFE8F5SHmISw+GtABibGCaAAhCiBidhlEMVj5jiloJ5YTiVAdvsjoMKZdrnJFLSnsB6ddGR8vqyAUDJJzNiIEbIOBxOvwAOQ24huqAINBMKxrcDQN12LooJC7FBuiFQmH0IA
π» Code
π Actual behavior
The value of
TContextis not inferred properly when an parameter is included in the builder function. Removing the argument resolves the issue, as does adding an explicit type for the argument.π Expected behavior
The value of
TContextshould be inferred properly, regardless of if a parameter is included in the builder function, or if the parameter has or does not have an explicit type.Additional information about the issue
No response