ts-extras
Version:
Essential utilities for TypeScript projects
32 lines (25 loc) • 875 B
TypeScript
import type { LastArrayElement } from 'type-fest';
/**
Return the last item of an array with stronger typing for tuples.
This provides better type safety than `array[array.length - 1]` or `array.at(-1)`.
@example
```
import {arrayLast} from 'ts-extras';
const tuple = ['abc', 123, true] as const;
const last = arrayLast(tuple);
//=> true
// ^? true
const array = ['a', 'b', 'c'];
const maybeLast = arrayLast(array);
//=> 'c'
// ^? string | undefined
// Empty arrays
const empty: string[] = [];
const noLast = arrayLast(empty);
//=> undefined
// ^? string | undefined
```
@category Improved builtin
*/
export declare function arrayLast<const ArrayType extends readonly [...unknown[], unknown]>(array: ArrayType): LastArrayElement<ArrayType>;
export declare function arrayLast<ArrayType extends readonly unknown[]>(array: ArrayType): ArrayType[number] | undefined;