Skip to content

Better type-safety for 6+ dimensions #2

@neefrehman

Description

@neefrehman

Currently this package uses overloads to safely type arrays of up to five dimensions, with a fallback generic return type, Matrix, for less common cases, which is:

type Matrix<T> = T[][][][][] | Matrix<T>[];

The overload that returns this type is:

function makeMatrix<T>(
    dimensions: number | number[],
    initialValues?: T
): Matrix<T>;

This type will ensure a depth of at least 5 dimensions when used, but beyond that won't provide type-safety for individual points. I have a feeling that there's a better way to do this, and have been looking into recursive and keyof types, but I'm struggling to get anywhere. Not even sure it's possible!

A fix for this would help ensure more type-safety across the board, and potentially allow me to not need overloads, as they currently bring their own UX problem where the first overload is suggested first, showing users that 1 | [number] can be passed in as the dimensions parameter, despite any number | number[] being usable. Placing the generic overload first would solve this, but then wouldn't allow the more specific return types to then take over as they are all still a subset of number | number[].

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions