Skip to content
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

Method concat has wrong typization #58631

Closed
phpcoder2022 opened this issue May 23, 2024 · 4 comments
Closed

Method concat has wrong typization #58631

phpcoder2022 opened this issue May 23, 2024 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@phpcoder2022
Copy link

🔎 Search Terms

typescript, concat, bug

🕗 Version & Regression Information

I tested this behavior on version 5.4.5. Local and playground results are equal.
I attached link to playground for demo.
You can see problem in output array. It isn't contains only arrays, but contains just numbers too.

⏯ Playground Link

https://www.typescriptlang.org/play/?jsx=0#code/C4TwDgpgBAcgrgWwEYQE4GUIEc4QHYDG0AvFHoiqgNoC6A3AFAMED2eAzsFMAIYDWEeMjTsopABQA6ae2y5CEdgC5YFNJhz4itAJRiAfFADeDKFFYcWAGwiSrLAObiABgEE8IKDwLAAlm1EAd19gAAsvVFQeEHZnABooWU0Fdh1GAF9GZgCuHgATPKFKdkxgUQlpSXJhDAgylQAFHiiEOpEAHlBIFgAzbn5BNVR2fV0DY1MyIdL2SR6WVABRb1DxWS5iQ14BIpEpGTqdNIZMpnzCoZK69nEqKgBGBIAmBIBmGhpJCwIeYFuAFgSAFYEgA2GhHOhAA

💻 Code

type NumberSequence = number[];

const takeNumbers = (...sequences: NumberSequence[]) => {
  console.log(`Any actions with arrays`, sequences);
};

const addNumbersSets = (...numberSets: Parameters<typeof takeNumbers>[]) => {
  numberSets.forEach(set => takeNumbers(...set));
};

addNumbersSets([[1, 2, 3]].concat([4, 5, 6]));

🙁 Actual behavior

Output array contains numbers

🙂 Expected behavior

Output array contains only arrays of numbers

Additional information about the issue

No response

@whzx5byb
Copy link

Duplicate of #19535.

@phpcoder2022
Copy link
Author

How for need generic type ConcatArray<T>? I wrote this type locally:

type ConcatArg<RawType> = RawType extends unknown[] ? RawType[] : (RawType | RawType[]);

interface Array<T> {
  concat(...items: ConcatArg<T>[]): T[];
}

Is it a acceptable solution?

@nmain
Copy link

nmain commented May 23, 2024

Is it a acceptable solution?

If it works for you, use it! My interpretation of #36554 is that the TypeScript team probably not interested in adopting such a change unless it's part of a holistic revamp of array typing issues, with consideration given to all users and possible breaking changes.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 6, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" 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 Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants