-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Improve Array.from(tuple)
and [...tuple]
#27859
Comments
Bump. The current behavior doesn't make any sense to me. Missing search keyword: spread, spreading |
I'm not seeing how this proposal addresses |
@jcalz I believe the proposed behavior for spreading tuples is:
@KSXGitHub Can you add this to the OP please? |
I will!
No, not declare const tuple: [0, 1, 2] & { foo: 'bar' } // typeof tuple → [0, 1, 2] & { foo: 'bar' }
const clone = [...tuple] // should be [0, 1, 2] and without { foo: 'bar' } |
By adding "Spread operator" section (as suggested by #27859 (comment) and #27859 (comment)), this issue is no longer within the boundary of |
It looks like #36861 handles tuple spread... but only on pure tuple types like declare const a: [0, 1, 2];
const b = [-2, -1, ...a, 3, 4, 5] as const;
// const b: readonly [-2, -1, 0, 1, 2, 3, 4, 5]
declare const c: [0, 1, 2] & { foo: "bar" };
const d = [-2, -1, ...c, 3, 4, 5] as const;
// const d: readonly (0 | 3 | 1 | 2 | -2 | -1 | 4 | 5)[] |
Looking at old issues, interestingly as of TS 4.2, the above example is now: declare const a: [0, 1, 2];
const b = [-2, -1, ...a, 3, 4, 5] as const;
// ^?
// const b: readonly [-2, -1, 0, 1, 2, 3, 4, 5]
declare const c: [0, 1, 2] & { foo: "bar" };
const d = [-2, -1, ...c, 3, 4, 5] as const;
// ^?
// const d: readonly [-2, -1, ...(0 | 1 | 2), 3, 4, 5] So, it gets the two ends correct. |
Suggestion
Array.from(tuple)
and[...tuple]
should preserve individual types that made up tuple.Proposal
Array.from
(Simple)Add this overload to
Array.from
:demo 1
Caveats:
Array.prototype
whilst actualArray.from
discards them. (for instance, if input hasfoo: 'bar'
, output array will also hasfoo: 'bar'
).Array.from
(Complete)Fix above caveats.
demo 2
Spread operator
Note that
typeof clone
does not contain{ foo: 'bar' }
.Use Cases
Examples
Clone a tuple
Clone a generic tuple
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: