Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose NonEmptyTuple type #915

Merged
merged 2 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();