Type alias InitialAndLast<Input>

InitialAndLast<Input>: Input extends readonly [...(infer InitialItems), infer LastItem]
    ? readonly [initial: InitialItems, last: LastItem]
    : Input extends EmptyArray | EmptyString
        ? readonly [initial: Input, last: undefined]
        : Input extends `${infer FirstCharacter}${infer RestOfString}`
            ? readonly [initial: `${RestOfString extends EmptyString
                ? EmptyString
                : FirstCharacter}${Head<InitialAndLast<RestOfString>>}`, last: `${RestOfString extends EmptyString
                ? FirstCharacter
                : Last<RestOfString>}`]
            : readonly [initial: Input, last: Maybe<Input[number]>]

Get a couple with the initial and last types of an ArrayLike.

Remarks

Given a ArrayLike, this returns a couple with the type of all items from the start until the item before last, and then the last.

Example

const array = ["🟢", "🟩", "💚"];
const initialAndLast: InitialAndLast<typeof array> = [["🟢", "🟩"], "💚"];

See

Type Parameters