TypeScript: `deleteCount` param to `splice()` is not optional in es5 types
TypeScript Version: 3.5.1
Search Terms: splice deleteCount
Code Set the target to ES5 then
[1, 2, 3].splice(0); // should error. deleteCount missing
[1, 2, 3].splice(0, 3); // ok
Expected behavior:
[1, 2, 3].splice(0); not allowed
Actual behavior:
[1, 2, 3].splice(0); allowed
Playground Link: this
deleteCount only become optional in ES6
ES5: https://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.12 ES6: https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.splice
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 15 (11 by maintainers)
Commits related to this issue
- make splice `deleteCount` required in es5.d.ts 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. ... — committed to tjenkinson/TypeScript by tjenkinson 5 years ago
- make splice `deleteCount` required in es5.d.ts 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. ... — committed to tjenkinson/TypeScript by tjenkinson 5 years ago
- make splice `deleteCount` required in es5.d.ts (#32643) * make splice `deleteCount` required in es5.d.ts In ES5 `deleteCount` is not an optional argument. If it is not provided it defaults to 0 as... — committed to microsoft/TypeScript by tjenkinson 4 years ago
Hi! I’ve run into a related issue. I’m not sure if this is really a bug with TypeScript, the interpreters, or the spec itself. However, the typings are at the very least misleading. ES2022 deleteCount was clarified to be clearly optional, but if you pass
undefinedas deleteCount splice() will fail silently. I noticed this when I wrapped splice with another class I was working on, which should work according to the typings.While deleteCount may or may not be optional (your guess is as good as mine), it cannot be
undefined.⏯ Playground Link
I added several cases to test behaviour at the following:
Playground link with relevant code
💻 Code
🙁 Actual behavior
Case 4: Arr length is now 4, and removed length is 0splice() failed silently here because undefined was passed as deleteCount.
🙂 Expected behavior
Case 4: Arr length is now 2, and removed length is 2According to the typings splice() should have worked.
Addendum
It looks like the PR was reverted, but this edge case doesn’t seem to be accounted for. @DanielRosenwasser and @RyanCavanaugh can you please take another look at this? It’s easy enough to code around, but this is unexpected / undocumented behaviour, and was a serious pain to track down.
I think this is what happens.
Maybe someone can correct me if I made a mistake.
(0, 0)(start, 0)(0, array.length)(start, array.length-actualStart)*From this, the
actualStartis computed,Thanks for the feedback on this BTW. When/if someone submits a PR it’d be nice if this issue had a clear and concrete description of what happens in ES5 vs ES6 with the various argument counts.