如何为嵌套在其中的其他数组的数组编写类型?



我有以下结构:

[
[
[0,0,0],
[1,1,1],
[0,0,0],
],
[
[0,1,0],
[0,1,0],
[0,1,0],
]
]

如何避免这种丑陋的符号?也许通过写类型…

allPatterns: number[][][] = [];

为清楚起见,您可以标记内部类型:

// Any set of three numbers
type MatrixRow = [number, number, number];
// Any set of three rows
type Matrix = [MatrixRow, MatrixRow, MatrixRow];
const exampleA: Matrix[] = [
[
[0,0,0],
[1,1,1],
[0,0,0],
], [
[0,1,0],
[0,1,0],
[0,1,0],
]
];
const exampleB: Matrix[] = [];

还可以为外部数组创建一个类型:

// A list of matrices
type MatrixSet = Matrix[];
const exampleC: MatrixSet = [];

打印稿操场

最新更新