Tuple
一句话解释
tuple 是数组类型使用 fixed 数字的 元素哪里每个位置有 特定, potentially 不同类型.
用大白话说
tuple 是数组使用 fixed 长度 and 特定类型 at 每个位置. 例如, [string, number] is a 2-element array where the first must be a string and the second must be a number.
Tuples differ from regular array types: 字符串[] is an array of any length containing strings, while [字符串, 数字] is exactly 2 elements with specific types at each index.
Tuples are useful for returning multiple values from a function, representing coordinate pairs, and defining CSV row structures. TypeScript also supports rest elements in tuples, like [字符串, ...数字[]] for a string followed by any number of numbers.
示例
typescript
let pair: [string, number] = ["age", 25];
console.log(pair[0]); // "age"
console.log(pair[1]); // 25tuple 有字符串 at 索引 0 and 数字 at 索引 1.
相关术语
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。