Skip to content

Contextually inferred generic function with generic rest parameter not assignable to itselfΒ #43833

Closed
@jcalz

Description

@jcalz

Bug Report

πŸ”Ž Search Terms

contextual, generic, inference, rest parameter, ts2345

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about generic inference

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function foo<A extends any[]>(
  arg: <T extends { a: number }>(t: T, ...rest: A) => number
) { }

foo((t, u: number) => t.a)
/* Argument of type
  '<T extends { a: number; }>(t: T, u: number) => number' is not assignable to parameter of type
  '<T extends { a: number; }>(t: T, u: number) => number'.
  πŸ€”πŸ€”πŸ€”
*/

πŸ™ Actual behavior

The compiler infers that (t, u: number) => t.a is of type <T extends {a: number}>(t: T, u: number) => number, but then complains that this type is not assignable to itself (or at least an identically written type). The problem seems to be that the two generic T's can't be unified for some reason.

πŸ™‚ Expected behavior

The compiler should infer the function type as it's doing now, but without futher complaint.


Comes from this Stack Overflow question. I don't really quite get the use case; presumably a real world application would have more than one site where T appears, like <T extends { a: number }>(t: T, ...rest: A) => T, which has the same problem.

Is there an existing issue that mentions this? Is it intended? Limitation? Genuine bug?

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Contextual TypesThe issue relates to contextual types

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions