PokéRogue
    Preparing search index...

    Type Alias TupleOf<Length, Fill>

    TupleOf: IfNotAnyOrNever<
        Length,
        _TupleOf<If<IsNegative<Length>, 0, Length>, Fill, []>,
        Fill[],
        [],
    >

    Create a tuple type of the specified length with elements of the specified type.

    Type Parameters

    • Length extends number
    • Fill = unknown
    import type {TupleOf} from 'type-fest';

    type RGB = TupleOf<3, number>;
    //=> [number, number, number]

    type Line = TupleOf<2, {x: number; y: number}>;
    //=> [{x: number; y: number}, {x: number; y: number}]

    type TicTacToeBoard = TupleOf<3, TupleOf<3, 'X' | 'O' | null>>;
    //=> [['X' | 'O' | null, 'X' | 'O' | null, 'X' | 'O' | null], ['X' | 'O' | null, 'X' | 'O' | null, 'X' | 'O' | null], ['X' | 'O' | null, 'X' | 'O' | null, 'X' | 'O' | null]]
    import type {TupleOf} from 'type-fest';

    type Range<Start extends number, End extends number> = Exclude<keyof TupleOf<End>, keyof TupleOf<Start>>;

    type ZeroToFour = Range<0, 5>;
    //=> '0' | '1' | '2' | '3' | '4'

    type ThreeToEight = Range<3, 9>;
    //=> '5' | '3' | '4' | '6' | '7' | '8'

    Note: If the specified length is the non-literal number type, the result will not be a tuple but a regular array.

    import type {TupleOf} from 'type-fest';

    type StringArray = TupleOf<number, string>;
    //=> string[]

    Note: If the type for elements is not specified, it will default to unknown.

    import type {TupleOf} from 'type-fest';

    type UnknownTriplet = TupleOf<3>;
    //=> [unknown, unknown, unknown]

    Note: If the specified length is negative, the result will be an empty tuple.

    import type {TupleOf} from 'type-fest';

    type EmptyTuple = TupleOf<-3, string>;
    //=> []

    Note: If you need a readonly tuple, simply wrap this type with Readonly, for example, to create readonly [number, number, number] use Readonly<TupleOf<3, number>>.