Add ArrayTail type (#913)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
This commit is contained in:
Tommy 2024-07-14 05:39:23 -05:00 committed by GitHub
parent bf85819fec
commit 128b21eed8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 40 additions and 6 deletions

1
index.d.ts vendored
View File

@ -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';

View File

@ -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.

25
source/array-tail.d.ts vendored Normal file
View File

@ -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 : [];

View File

@ -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.

View File

@ -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';

12
test-d/array-tail.ts Normal file
View File

@ -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));