Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inference of builder function fails when function parameter is included #60720

Closed
mskelton opened this issue Dec 9, 2024 · 4 comments Β· May be fixed by #60909
Closed

Inference of builder function fails when function parameter is included #60720

mskelton opened this issue Dec 9, 2024 · 4 comments Β· May be fixed by #60909
Labels
Duplicate An existing issue was already created

Comments

@mskelton
Copy link

mskelton commented Dec 9, 2024

πŸ”Ž 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

@Andarist
Copy link
Contributor

Andarist commented Dec 9, 2024

I think this is a duplicate of this closed issue: #57021 . I have an experimental branch lying around somewhere that could address - I'll try to revamp it and push out soon-ish

@RyanCavanaugh
Copy link
Member

See #47599

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 11, 2024
@mskelton
Copy link
Author

Will close as duplicate and I'll follow those other issues. Thanks

@mskelton mskelton closed this as not planned Won't fix, can't repro, duplicate, stale Dec 12, 2024
@Andarist
Copy link
Contributor

Andarist commented Jan 3, 2025

an experimental PR to fix this: #60909

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants