Type alias Transpose<M, N>

Transpose<M, N>: M extends []
    ? []
    : {
        [KN in Keys<N>]: {
            [KM in Keys<M>]: KN extends Keys<M[KM]>
                ? M[KM][KN]
                : never
        }
    }

Transposes a matrix (2D array) by converting rows into columns and columns into rows

Type Parameters

  • M extends Primitive[][]

    The input matrix type (2D array of primitives)

  • N extends Primitive[] = M[0]

    Helper type representing the first row of the matrix (default: M[0])

Example

type Matrix = [
[1, 2, 3],
[4, 5, 6]
];
Transpose<Matrix>; // Result: [[1, 4], [2, 5], [3, 6]]