microsoft / microsoft/TypeScript

Add `NonEmptyArray` to `lib` (but **not** `length > 0` narrowing)

Open
#60,491 6 comments 0 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

⚙ Compilation target

ES2023

⚙ Library

ES2023

Missing / Incorrect Definition
type NonEmptyArray<T> = [T, ...T[]]
type ReadonlyNonEmptyArray<T> = readonly [T, ...readonly T[]]
Sample Code
const is_non_empty = <T>(a: ReadonlyArray<T>): a is ReadonlyNonEmptyArray<T> =>
	a.length > 0

function sum(a: ReadonlyNonEmptyArray<number>): number
function sum(a: ReadonlyNonEmptyArray<bigint>): bigint
function sum(a:
	ReadonlyNonEmptyArray<number> |
	ReadonlyNonEmptyArray<bigint>
) {
	//@ts-expect-error
	return a.reduce((acc, x) => acc + x)
}
Documentation Link

There's no docs that I'm aware of. However, there are multiple issues using this boilerplate. See also: #60463

Here's a WIP example implementations of sum. The sample code is a simplified version of that. More info here

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 locating the ES2023 library declaration files and the existing array type definitions; the issue names no specific file or test. Add the mutable and readonly NonEmptyArray aliases without introducing length-based narrowing, then verify the supplied type-checking example and relevant compiler tests.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.