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

Most upvoted comments

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 undefined as 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

class FailingWrapper{
  arr = [1,2,3,4];

  splice(start:number, deleteCount?: number){
    // When deleteCount is 'undefined' method fails silently
    return this.arr.splice(start, deleteCount);
  }
}

const wrapper = new FailingWrapper();
const removed = wrapper.splice(2) // deleteCount is undefined

console.log(
  `Case 4: Arr length is now ${wrapper.arr.length}, and removed length is ${removed.length}`
);

🙁 Actual behavior

Case 4: Arr length is now 4, and removed length is 0

splice() failed silently here because undefined was passed as deleteCount.

🙂 Expected behavior

Case 4: Arr length is now 2, and removed length is 2

According 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.

ES array.splice() array.splice(start)
ES5 (0, 0) (start, 0)
ES6 (0, array.length) (start, array.length-actualStart) *

From this, the actualStart is computed,

  1. Let relativeStart be ToInteger(start).
  2. ReturnIfAbrupt(relativeStart).
  3. If relativeStart < 0, let actualStart be max((len + relativeStart),0); else let actualStart be min(relativeStart, len).

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.