-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow array concat
to concat arrays of different types
#31355
Comments
concat
to concat different types
concat
to concat different typesconcat
to concat arrays of different types
You can always use declaration merging to add a generic overload to // declaration merge
interface Array<T> {
concat<U>(...items: (U | ConcatArray<U>)[]): (T | U)[]
}
const okay = [0].concat(""); // Array<string | number> Or, you can widen the array element type yourself before const okay = [0 as string | number].concat(''); // Array<string | number> Before the standard library is modified for everyone, though, you'd want to establish that this use case is common enough in real-world code to warrant it. The compiler tries to catch potential bugs. For real uses of |
Duplicate #26378 |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
I have hit this issue while developing a Vue.js component that accepts a It is an option-list type component that allows a user to select either a single or multiple options from a given list, the options being indexed by string or number ids. The reason I'm not only accepting arrays as the Within the component however, it is handy to always work with the prop as an array, so I created a simple getter using the `[].concat()' trick. No biggie in vanilla JS but turned out to be an issue with TS. I have dealt with it like so:
|
TypeScript Version: 3.5.0-dev.20190511
Code
Expected behavior:
The concated array has type
(number|string)[]
.Actual behavior:
Error.
Possible fix:
In
TypeScript/lib/lib.es5.d.ts
Line 1231 in b7fe99a
Playground Link:
https://www.typescriptlang.org/play/#src=%5B0%5D.concat(%5B''%5D)
Related Issues:
#29604
Search Terms:
array concat
The text was updated successfully, but these errors were encountered: