Skip to content

Parameters in functions with type parameters are not contextually-typedΒ #61791

Open
@Andarist

Description

@Andarist

πŸ”Ž Search Terms

type parameters generic conextual parameter types context sensitive

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250601#code/KYDwDg9gTgLgBAE2AYwDYEMrDsiA7AZ3gDM8AuOAHgBUAaOAQSgHMC5QZg8E2mp0AnpXR4BAPnoAlYDDEAKALAAoOHALBUxCnWWqARhAQCKcmAAsAlgW30AdPcysKTVgEo4AXjFwA4l2D8MNDColIy9HjAAG4BYsruXnBy9raO1owsBAne0jDKyqCQsDj4RHAQANaecKRyAN5wALbABATozMAUAETEEBBdcAC+9MQArnjIMBb4AFRy5HB4o416Ae51uiWEEKjAtqgQzKaWBLbNre3AEa7KgzdKBeDQ8LiE8HgQ1bUN520d3b1+kMRuNJtM8DNKAA5eQLKHrTavAg7PYHI7mKxnFp-K6Le53ZRAA

πŸ’» Code

export declare const fn: <T, Args extends Array<any>, Ret>(
  self: T,
  body: (this: T, ...args: Args) => Generator<any, Ret, never>
) => (...args: Args) => Ret

export const ok = fn({ message: "foo" }, function*(n: number) {
  console.log(this.message, n) // ok
})

export const no = fn({ message: "foo" }, function*<N>(n: N) {
  console.log(this.message, n) // error
})

πŸ™ Actual behavior

The second case errors

πŸ™‚ Expected behavior

Both of those should work fine

Additional information about the issue

Given TS is able (at times) to infer new generic signatures based on generic function arguments (notice the return type of the failing case: const no: <N>(n: N) => void), it makes sense to assign contextual parameter types.

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions