Skip to content

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

Closed as not planned
@mskelton

Description

@mskelton

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions