Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
KingMario opened this issue Sep 19, 2023 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@KingMario
Copy link

KingMario commented Sep 19, 2023

πŸ”Ž 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

@MartinJohns
Copy link
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).

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Sep 19, 2023
@KingMario
Copy link
Author

KingMario commented Sep 19, 2023

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.

@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 22, 2023
@KingMario
Copy link
Author

Hi @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants