# 定义固定长度的数组

来源: <https://stackoverflow.com/questions/52489261/typescript-can-i-define-an-n-length-tuple-type>

Typescript 3 introduces [rest elements in tuple types](https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#rest-elements-in-tuple-types)

> The last element of a tuple type can be a rest element of the form ...X, where X is an array type

To restrict the length of a tuple we can use intersection with `{ length: N }`

定义类型

```typescript
type Tuple<TItem, TLength extends number> = [TItem, ...TItem[]] & { length: TLength };
```

那么定长为6的数组就是

```typescript
let matrix: Tuple<number, 6>
matrix = [1,2,3,4,5,6]
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mm.ricky.moe/framework/react/ding-yi-gu-ding-chang-du-de-shu-zu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
