Skip to content

Commit

Permalink
Add NonEmptyTuple type (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell authored Jul 14, 2024
1 parent 128b21e commit bb57638
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export type {IsNull} from './source/is-null';
export type {IfNull} from './source/if-null';
export type {And} from './source/and';
export type {Or} from './source/or';
export type {NonEmptyTuple} from './source/non-empty-tuple';

// Template literal types
export type {CamelCase} from './source/camel-case';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Click the type names for complete docs.
- [`DistributedPick`](source/distributed-pick.d.ts) - Picks keys from a type, distributing the operation over a union.
- [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both true.
- [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types are true.
- [`NonEmptyTuple`](source/non-empty-tuple.d.ts) - Matches any non-empty tuple.

### Type Guard

Expand Down
5 changes: 0 additions & 5 deletions source/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,6 @@ Matches any unknown array or tuple.
*/
export type UnknownArrayOrTuple = readonly [...unknown[]];

/**
Matches any non empty tuple.
*/
export type NonEmptyTuple = readonly [unknown, ...unknown[]];

/**
Returns a boolean for whether the two given types extends the base type.
*/
Expand Down
2 changes: 1 addition & 1 deletion source/merge-deep.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type {Merge} from './merge';
import type {
FirstArrayElement,
IsBothExtends,
NonEmptyTuple,
UnknownArrayOrTuple,
} from './internal';
import type {NonEmptyTuple} from './non-empty-tuple';
import type {ArrayTail} from './array-tail';
import type {UnknownRecord} from './unknown-record';
import type {EnforceOptional} from './enforce-optional';
Expand Down
21 changes: 21 additions & 0 deletions source/non-empty-tuple.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
Matches any non-empty tuple.
@example
```
import type {NonEmptyTuple} from 'type-fest';
const sum = (...numbers: NonEmptyTuple<number>) => numbers.reduce((total, value) => total + value, 0);
sum(1, 2, 3);
//=> 6
sum();
//=> Error: Expected at least 1 arguments, but got 0.
```
@see {@link RequireAtLeastOne} for objects
@category Array
*/
export type NonEmptyTuple<T = unknown> = readonly [T, ...T[]];
10 changes: 10 additions & 0 deletions test-d/non-empty-tuple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {expectType} from 'tsd';
import type {NonEmptyTuple} from '../index';

declare const sum: (...numbers: NonEmptyTuple<number>) => number;

expectType<number>(sum(1, 2, 3));
expectType<number>(sum(1));

// @ts-expect-error
sum();

0 comments on commit bb57638

Please sign in to comment.