microsoft / microsoft/TypeScript

Add overload to `unshift` to catch calling it with zero elements

Open
#43,244 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

lib Update Request

I often mistake unshift for shift; calling array.unshift() with zero arguments is almost always a mistake, and I'd like it to tell me that.

interface Array<T> {
    /**
     * @deprecated You probably meant to use `.shift()`
     */
    unshift(): number;
    /**
     * Inserts new elements at the start of an array, and returns the new length of the array.
     * @param items Elements to insert at the start of the array.
     */
    unshift(...items: T[]): number
}

Configuration Check

My compilation target is ESNext and my lib is the default.

Missing / Incorrect Definition

Array.prototype.unshift

Sample Code

const myArray = ["abc"];

// This should error / warn, as this is almost definitely a mistake
myArray.unshift()

// This should be ok even though the array might have zero elements
myArray.unshift(...myOtherArray)

Documentation Link

Not fully relevant: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the standard library declaration for Array.prototype.unshift, identified by the issue, and review nearby overload conventions. Check the existing type-checking tests for array methods; done means a direct zero-argument call is flagged while spreading another array remains valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.