question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
k-dillcommented, Nov 19, 2021

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.

1reaction
AnyhowStepcommented, Jul 31, 2019

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).
Read more comments on GitHub >

github_iconTop Results From Across the Web

Array.prototype.splice() - JavaScript - MDN Web Docs
The splice() method changes the contents of an array by removing or replacing ... If deleteCount is 0 or negative, no elements are...
Read more >
An In-Depth Exploration of the Array.splice() Function
The splice() function is called with a start parameter value of 2 , a deleteCount parameter value of 0 , and a single...
Read more >
Array splice method with non number argument - Stack Overflow
splice (0) removes all elements because if the 2nd parameter deleteCount is omitted, it will be equal to array.length - start -> in...
Read more >
JavaScript Basic Array Methods - GeeksforGeeks
deleteCount : Number of elements to be deleted, if no element is to be deleted pass 0. Item1, item2 …..: this is an...
Read more >
List | DynamoDB Client - AWS SDK for JavaScript v3
Returns the value of the first element in the array where predicate is true, and undefined otherwise. Type parameters. S: Value. Parameters ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found