Skip to content

Generic function with rest parameters does not infer or widen actual args to union type #37673

Closed
@ghost

Description

TypeScript Version: 3.8.3

Search Terms:
generic function rest parameters

Code

// A *self-contained* demonstration of the problem follows...

function foo<T extends string | number>(...rest: T[]): T[] {
  return rest;
}

foo('a', 1);

Expected behavior:
Should compile without error and infer the return type of foo('a', 1) to be at least
(string | number)[] or, better still, more accurately as ("a" | 1)[]

Actual behavior:

src/main.ts:5:10 - error TS2345: Argument of type '1' is not assignable to parameter of type '"a"'.
5 foo('a', 1);

Related Issues:

Looked at #37052 for a clue. The only thing there was a similar error message to mine above but otherwise probably a red herring to say that it's similar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions