Skip to content

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

Closed

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.

Activity

  1. RyanCavanaugh commented on Apr 1, 2020

    @RyanCavanaugh
    Member

    This is consistent with the non-rest version:

    function fn<T extends string | number>(a: T, b: T) { }
    fn('a', 1);
  2. typescript-bot commented on Apr 3, 2020

    @typescript-bot
    Contributor

    This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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