microsoft / microsoft/TypeScript

Array.length type guard for array spreading

Open
#28,837 10 comments 28 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

TypeScript Version: 3.3.0-dev.20181204

Search Terms: array length type guard spread

Code

const timeString = '09:00';
const date = new Date();
const timeParts = timeString.split(':').map((part) => parseInt(part, 10));
if (timeParts.length > 0) {
    date.setHours(...timeParts);
}

Expected behavior: No errors

Actual behavior: TS2556: Expected 1-4 arguments, but got 0 or more.

Playground Link: https://www.typescriptlang.org/play/#src=const%20timeString%20%3D%20'09%3A00'%3B%0D%0Aconst%20date%20%3D%20new%20Date()%3B%0D%0Aconst%20timeParts%20%3D%20timeString.split('%3A').map((part)%20%3D%3E%20parseInt(part%2C%2010))%3B%0D%0Aif%20(timeParts.length%20%3E%200)%20%7B%0D%0A%20%20%20%20date.setHours(...timeParts)%3B%0D%0A%7D%0D%0A

Related Issues: None

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 by reproducing the TypeScript 3.3.0-dev.20181204 example from the linked Playground, focusing on array spreading after an Array.length check. Trace the compiler's array-spread argument checking and length narrowing, then add a regression test and confirm the guarded spread no longer reports TS2556.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.