-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Fix __spreadArray for non-concat-spreadables #45386
Conversation
@@ -647,7 +647,7 @@ namespace ts { | |||
ar[i] = from[i]; | |||
} | |||
} | |||
return to.concat(ar || from); | |||
return to.concat(ar || Array.prototype.slice.call(from)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how crazy we want to get with this one but we could also do
return to.concat(ar || Array.prototype.slice.call(from)); | |
return to.concat(ar || (from instanceof Array ? from : Array.prototype.slice.call(from))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered that, including a check for isConcatSpreadable
, but it just became long-winded and the performance benefits seemed negligible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was originally thinking of doing this:
var __spreadArray = (this && this.__spreadArray) || (function () {
var toString = Object.prototype.toString;
var slice = Array.prototype.slice;
var isArray = typeof Array.isArray === "function" ? Array.isArray : function (obj) { return toString.call(obj) === "[object Array]"; };
var isConcatSpreadable = typeof Symbol === "function" && Symbol.isConcatSpreadable;
function canConcat(obj) { return isArray(obj) || isConcatSpreadable && obj[isConcatSpreadable]; }
return function (to, from, pack) {
if (pack || arguments.length === 2) {
for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = slice.call(from, 0, i);
ar[i] = from[i];
}
}
if (ar) from = ar;
}
return to.concat(canConcat(from) ? from : slice.call(from));
}
})();
We'll need to cherry-pick this in for release-4.4, so ping me when you're ready to merge. |
Also, take a look at microsoft/tslib#155 to fix this on the tslib side, since the concat-spreadable thing is a back-compat issue. |
@typescript-bot cherry-pick this to release-4.4 |
Heya @DanielRosenwasser, I've started to run the task to cherry-pick this into |
@typescript-bot cherry-pick this to release-4.4 and LKG |
Heya @DanielRosenwasser, I've started to run the task to cherry-pick this into |
Hey @DanielRosenwasser, I've opened #45416 for you. |
Component commits: 7c7e0eb Fix __spreadArray for non-concat-spreadables
Hey @DanielRosenwasser, I couldn't open a PR with the cherry-pick. (You can check the log here). You may need to squash and pick this PR into release-4.4 manually. |
Component commits: 7c7e0eb Fix __spreadArray for non-concat-spreadables
The
__spreadArray
helper usesArray.concat
, which doesn't work with DOM NodeList or HTMLCollection. This changes the__spreadArray
helper to fall back toArray.prototype.slice.call(from)
to ensure the elements offrom
are concatenated properly.Fixes TypeScript side of microsoft/tslib#153