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 ArrayTail type #913

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 @@ -122,6 +122,7 @@ export type {ArrayIndices} from './source/array-indices';
export type {ArrayValues} from './source/array-values';
export type {ArraySlice} from './source/array-slice';
export type {ArraySplice} from './source/array-splice';
export type {ArrayTail} from './source/array-tail';
export type {SetFieldType} from './source/set-field-type';
export type {Paths} from './source/paths';
export type {SharedUnionFieldsDeep} from './source/shared-union-fields-deep';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Click the type names for complete docs.
- [`ArrayIndices`](source/array-indices.d.ts) - Provides valid indices for a constant array or tuple.
- [`ArrayValues`](source/array-values.d.ts) - Provides all values for a constant array or tuple.
- [`ArraySplice`](source/array-splice.d.ts) - Creates a new array type by adding or removing elements at a specified index range in the original array.
- [`ArrayTail`](source/array-tail.d.ts) - Extracts the type of an array or tuple minus the first element.
- [`SetFieldType`](source/set-field-type.d.ts) - Create a type that changes the type of the given keys.
- [`Paths`](source/paths.d.ts) - Generate a union of all possible paths to properties in the given object.
- [`SharedUnionFieldsDeep`](source/shared-union-fields-deep.d.ts) - Create a type with shared fields from a union of object types, deeply traversing nested structures.
Expand Down
25 changes: 25 additions & 0 deletions source/array-tail.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type {UnknownArrayOrTuple} from './internal';

/**
Extracts the type of an array or tuple minus the first element.

@example
```
import type {ArrayTail} from 'type-fest';

declare const curry: <Arguments extends unknown[], Return>(
function_: (...arguments_: Arguments) => Return,
...arguments_: ArrayTail<Arguments>
) => (...arguments_: ArrayTail<Arguments>) => Return;

const add = (a: number, b: number) => a + b;

const add3 = curry(add, 3);

add3(4);
//=> 7
```

@category Array
*/
export type ArrayTail<TArray extends UnknownArrayOrTuple> = TArray extends readonly [unknown, ...infer Tail] ? Tail : [];
5 changes: 0 additions & 5 deletions source/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,6 @@ export type FirstArrayElement<TArray extends UnknownArrayOrTuple> = TArray exten
? THead
: never;

/**
Extracts the type of an array or tuple minus the first element.
*/
export type ArrayTail<TArray extends UnknownArrayOrTuple> = TArray extends readonly [unknown, ...infer TTail] ? TTail : [];

/**
Extract the element of an array that also works for array union.

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 @@ -3,12 +3,12 @@ import type {OmitIndexSignature} from './omit-index-signature';
import type {PickIndexSignature} from './pick-index-signature';
import type {Merge} from './merge';
import type {
ArrayTail,
FirstArrayElement,
IsBothExtends,
NonEmptyTuple,
UnknownArrayOrTuple,
} from './internal';
import type {ArrayTail} from './array-tail';
import type {UnknownRecord} from './unknown-record';
import type {EnforceOptional} from './enforce-optional';
import type {SimplifyDeep} from './simplify-deep';
Expand Down
12 changes: 12 additions & 0 deletions test-d/array-tail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {expectType} from 'tsd';
import type {ArrayTail} from '../index';

declare const getArrayTail: <T extends readonly unknown[]>(array: T) => ArrayTail<T>;

expectType<[]>(getArrayTail([]));
expectType<[]>(getArrayTail(['a']));
expectType<[]>(getArrayTail(['a', 'b', 'c']));

expectType<[]>(getArrayTail([] as const));
expectType<[]>(getArrayTail(['a'] as const));
expectType<['b', 'c']>(getArrayTail(['a', 'b', 'c'] as const));