`deleteCount` param to `splice()` is not optional in es5 types
See original GitHub issueTypeScript 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:
- Created 4 years ago
- Comments:14 (11 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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
π 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.
(0, 0)
(start, 0)
(0, array.length)
(start, array.length-actualStart)
*From this, the
actualStart
is computed,