> For the complete documentation index, see [llms.txt](https://willh.gitbook.io/typescript-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://willh.gitbook.io/typescript-tutorial/advanced/tuple.md).

# 元組

數組合並了相同型別的物件，而元組（Tuple）合併了不同型別的物件。

元組起源於函式程式語言（如 F#），這些語言中會頻繁使用元組。

## 簡單的例子

定義一對值分別為 `string` 和 `number` 的元組：

```typescript
let tom: [string, number] = ['Tom', 25];
```

當賦值或訪問一個已知索引的元素時，會得到正確的型別：

```typescript
let tom: [string, number];
tom[0] = 'Tom';
tom[1] = 25;

tom[0].slice(1);
tom[1].toFixed(2);
```

也可以只賦值其中一項：

```typescript
let tom: [string, number];
tom[0] = 'Tom';
```

但是當直接對元組型別的變數進行初始化或者賦值的時候，需要提供所有元組型別中指定的項。

```typescript
let tom: [string, number];
tom = ['Tom', 25];
```

```typescript
let tom: [string, number];
tom = ['Tom'];

// Property '1' is missing in type '[string]' but required in type '[string, number]'.
```

## 越界的元素

當新增越界的元素時，它的型別會被限制為元組中每個型別的聯合型別：

```typescript
let tom: [string, number];
tom = ['Tom', 25];
tom.push('male');
tom.push(true);

// Argument of type 'true' is not assignable to parameter of type 'string | number'.
```

## 參考

* [Basic Types # Tuple](http://www.typescriptlang.org/docs/handbook/basic-types.html#tuple)（[中文版](https://zhongsp.gitbooks.io/typescript-handbook/content/doc/handbook/Basic%20Types.html#元組-tuple)）
* [上一章：字串字面量型別](/typescript-tutorial/advanced/string-literal-types.md)
* [下一章：列舉](/typescript-tutorial/advanced/enum.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://willh.gitbook.io/typescript-tutorial/advanced/tuple.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
