Skip to content

Type definition with two rest operations for two arrays doesn't validated correctlyΒ #55783

Closed as not planned

Description

πŸ”Ž Search Terms

rest array

πŸ•— Version & Regression Information

N/A

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAggTnAhiA6gS2ACwHIFEA2EAthAHbADOAPACoA0U2UEAHsGQCYVSkCuRAIwhwAfFAC8UGgG0AulABkUAN6FSAcywAuRgF8A3AChDoSLATJ0WbAAV8vCgWJlKtBk1btSXHvyGiJKGkAOlD4JFQMHCcScmp6RhEGUOCZWVkjE3BoAFkQcORAgsjrOwcYl2o+QWEGABYRTIBjAHtSCmAoRAQABgBGHTziwOk+hgAmBgBmDMMgA

πŸ’» Code

type ArrayWithNElements<T, N extends number> = T[] & {length: N};

type ArrayWithNPlusElements<T, N extends number> = [...ArrayWithNElements<T, N>, ...T[]];

type MyArray = ArrayWithNPlusElements<number, 4>;

const arr01: MyArray = [1, 2, 3];

πŸ™ Actual behavior

const arr01: MyArray = [1, 2, 3]; in the code is not validated as error.

While in the following code, arr02 is validated as error.

type NumberX4 = [number, number, number, number];
type MyArray2 = [...NumberX4, ...number[]];

const arr02: MyArray2 = [1, 2, 3];

πŸ™‚ Expected behavior

const arr01: MyArray = [1, 2, 3]; in the code should be validated as error.

Validation of MyArray should be an attempt of splitting it into two arrays to validate against each array rest operation.

Additional information about the issue

No response

Activity

  1. MartinJohns commented on Sep 19, 2023

    @MartinJohns
    Contributor

    T[] & {length: N} is not a valid tuple type, it's an array. So when you spread it, it's spread as an array, and your ArrayWithNPlusElements<T, N> type ends up being just T[]. You can see this when hovering over the type. When you use a proper tuple type you don't have this issue, that's why the suggested solution for the "N Tuple type" is #54740 (comment).

  2. KingMario commented on Sep 19, 2023

    @KingMario
    Author

    Thanks MartinJohns

    I'm just reporting a bug that

    either

    typescript should be able to report an error while declaring the type ArrayWithNPlusElements<T, N> as the error reported for type MixedArray<T, U> = [...T[], ...U[]], which is, A rest element cannot follow another rest element.,

    or

    typescript should be able to validate arr01 as error.

  3. typescript-bot commented on Sep 22, 2023

    @typescript-bot
    Contributor

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

  4. KingMario commented on Sep 22, 2023

    @KingMario
    Author

    Hi Ryan Cavanaugh (@RyanCavanaugh)

    IMHO, this is not a question. It's the buggy behavior of typescript compiler/language service that

    either

    typescript should be able to report an error while declaring the type ArrayWithNPlusElements<T, N> as the error reported for type MixedArray<T, U> = [...T[], ...U[]], which is, A rest element cannot follow another rest element.,

    or

    typescript should be able to validate arr01 as error.

    @MartinJohns I can't @ you because you blocked me. But I'm not talking about Tuple at all as a subset of the Array type. I don't use Typle much because I think it's immature, whereas you can call pop so that the tuple type of [string, string] becomes [string] or [] without realizing it.

  5. locked as resolved and limited conversation to collaborators on Oct 22, 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

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions