Skip to content

make splice deleteCount required in es5.d.ts - #32643

Merged
Nathan Shively-Sanders (sandersn) merged 3 commits into
microsoft:masterfrom
tjenkinson:es5-splice-delete-count
Mar 12, 2020
Merged

make splice deleteCount required in es5.d.ts#32643
Nathan Shively-Sanders (sandersn) merged 3 commits into
microsoft:masterfrom
tjenkinson:es5-splice-delete-count

Conversation

@tjenkinson

@tjenkinson Tom Jenkinson (tjenkinson) commented Jul 31, 2019

Copy link
Copy Markdown
Contributor

In ES5 deleteCount is not an optional argument. If it is not provided it defaults to 0 as a side effect of undefined being converted to an integer.
https://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.12

In ES6 deleleteCount is optional, and it defaults to the length of the array minus the start index.
https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.splice

If you are targeting ES5 but don't provide deleteCount the behaviour will be different depending on the environment your build is running in.

This PR makes the deleteCount required when targeting ES5 but should allow it to be optional for ES6.

fixes #32638

The Issue

When running in ES5

const input = [1, 2, 3];
const out = in.splice(0);
console.log(input); // [1, 2, 3]
console.log(out); // []

When running in ES6

const input = [1, 2, 3];
const out = in.splice(0);
console.log(input); // []
console.log(out); // [1, 2, 3]

@msftclas

Microsoft Contribution License Agreements (msftclas) commented Jul 31, 2019

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

In ES5 `deleteCount` is not an optional argument. If it is not provided it defaults to 0 as a side effect of `undefined` being converted to an integer.

In ES6 `deleleteCount` is optional, and it defaults to the length of the array minus the start index.

If you are targeting ES5 but don't provide `deleteCount` the behaviour will be different depending on the environment your build is running in.

fixes microsoft#32638
@RyanCavanaugh

Copy link
Copy Markdown
Member

@typescript-bot

TypeScript Bot (typescript-bot) commented Jul 31, 2019

Copy link
Copy Markdown
Contributor

Heya Ryan Cavanaugh (@RyanCavanaugh), I've started to run the parallelized community code test suite on this PR at 368d007. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot

Copy link
Copy Markdown
Contributor

Heya Ryan Cavanaugh (@RyanCavanaugh), I'm starting to run the extended test suite on this PR at 368d007. Hold tight - I'll update this comment with the log link once the build has been queued.

@typescript-bot

Copy link
Copy Markdown
Contributor

The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master.

@RyanCavanaugh

Copy link
Copy Markdown
Member

Wesley Wigham (@weswigham) looks like some noise in the user suite diff - can you check in on it?

@weswigham

Copy link
Copy Markdown
Member

I opened #32610 to fix the chrome devtools changes awhile ago, the other changes (to the azure sdk which is happily updating rush and such under us, and to zone.js which seems to be broken slightly (no ides if that's our fault)) just need to be accepted.

@tjenkinson

Copy link
Copy Markdown
Contributor Author

Anything I can do to help here?

@tjenkinson

Tom Jenkinson (tjenkinson) commented Jan 17, 2020

Copy link
Copy Markdown
Contributor Author

@sandersn Nathan Shively-Sanders (sandersn) added the For Backlog Bug PRs that fix a backlog bug label Feb 1, 2020
@tjenkinson

Copy link
Copy Markdown
Contributor Author

I brought this back up to date with master

@sandersn

Copy link
Copy Markdown
Member

Thanks for your patience on this!

@sandersn
Nathan Shively-Sanders (sandersn) merged commit ddcf139 into microsoft:master Mar 12, 2020
@RyanCavanaugh

Copy link
Copy Markdown
Member

This caused a break

const arr: string[] = [];
const boundSplice = arr.splice.bind(arr, 0, 0);
const items: string[] = [];
// Used to be OK, now an error
boundSplice.apply(arr, items);

@ljharb

Copy link
Copy Markdown
Contributor

Ryan Cavanaugh (@RyanCavanaugh) it's OK in ES6, it's not OK in ES5 (altho there may have been pre-ES6 browsers that considered it optional). is that a type error when targeting ES6?

@RyanCavanaugh

Copy link
Copy Markdown
Member

It's incorrectly a type error right now against ES6

@tjenkinson

Tom Jenkinson (tjenkinson) commented Apr 24, 2020

Copy link
Copy Markdown
Contributor Author

Shame this is getting reverted. We had an outage on our TV app which this would have caught if it had existed. A few TV’s are still way back in the past.

@tjenkinson

Copy link
Copy Markdown
Contributor Author

Do you know why it caused bind and apply to break?

@ljharb

Copy link
Copy Markdown
Contributor

Ryan Cavanaugh (@RyanCavanaugh) so does that mean this PR needed to also override it in ES6 to make it optional?

@DanielRosenwasser

Copy link
Copy Markdown
Member

Tom Jenkinson (@tjenkinson) I believe it's because under strictBindCallApply, we try to infer the types from the signature, but overloads don't play well with that functionality. So Jordan Harband (@ljharb), if you had ES6 as the target, that extra overload caused issues. We believe that the most reasonable solution is to just say it's optional.

@tjenkinson

Copy link
Copy Markdown
Contributor Author

Ok thanks for the explanation.

It’s a shame but not the end of the world because es5-shim protects us now :). I guess we could also add the definition to our projects manually.

@ljharb

Copy link
Copy Markdown
Contributor

Daniel Rosenwasser (@DanielRosenwasser) it seems like fixing the underlying flaws with overloads is a pretty important thing to address :-/

@DanielRosenwasser

Copy link
Copy Markdown
Member

I agree, but I don't think we have anything concrete that we feel good about yet.

@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

deleteCount param to splice() is not optional in es5 types

8 participants