蓝色天空网mbsky.com · 知识 · 实践 · 创造TypeScript · 本地阅读预览
教程 / 前端开发 / TypeScript

TypeScript 元组

按位置建模元组,覆盖访问、修改、push/pop、解构、连接、切片、遍历和可变长度扩展的真实约束。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

混合元素不一定需要元组

typescript · 阅读源码
const mixed = [42, "学习"]; // 通常是(number | string)[]
const tuple: [number, string, boolean] = [42, "蓝色天空网", true];
const readonlyTuple: readonly [id: number, title: string] = [7, "TypeScript"];

元组表达“第几个位置是什么含义和类型”,普通数组也能用联合元素类型保存混合值。裸写[42,"Hello"]并不会自动成为固定位置元组;使用注解、明确上下文或as const。标签id/title提升可读性,但不会创建.id或.title属性,也不参与不同标签之间的运行身份。

访问、更新、解构和多返回值

typescript · 阅读源码
let entry: [number, string, boolean] = [42, "学习", true];
console.log(entry[0], entry[1], entry[2]);
entry[0] = 121;
const [id, title] = entry;
function userInfo(): [id: number, name: string] { return [7, "蓝色天空网"]; }
const [userId, userName] = userInfo();

已知合法字面量索引可得到位置类型,固定长度元组访问越界索引会被诊断。非固定数字索引常得到可能成员的联合;可选位置还要考虑undefined。更新要满足对应位置类型,readonly元组则禁止经此引用修改。

push与pop不保持所有固定长度保证

typescript · 阅读源码
let entry: [number, string, boolean] = [42, "学习", true];
const last = entry.pop();
console.log(last, entry.length);
// 运行时现在只有两个元素;不要继续假定entry[2]仍存在。

可变元组继承数组方法,push可能接受其元素类型联合,pop可能返回成员联合或undefined;这些方法不会把变量静态类型自动重算成新的精确长度。示例里pop实际得到true,不代表pop声明返回永远是boolean。需要固定合同通常暴露readonly元组;需要增长则显式使用rest元组或普通数组。

连接、切片、遍历与转换

typescript · 阅读源码
const first: [number, string] = [42, "学习"];
const second: [boolean, number] = [true, 100];
const combined: [number, string, boolean, number] = [...first, ...second];
const sliced = combined.slice(1);
const array = Array.from(combined);
for (const item of combined) console.log(item);
combined.forEach(item => console.log(item));

concat/slice/Array.from通常产生普通数组,且concat的元素约束可能拒绝直接连接不兼容成员的元组,不能保证tuple1.concat(tuple2)总可编译。用带目标类型的展开或泛型可变元组表达确实要保留的位置关系。转换后的数组允许的push值仍受元素类型联合限制。

只读、可选、rest与常量断言

typescript · 阅读源码
type Coordinate = readonly [x: number, y: number];
type Named = [name: string, visits?: number];
type Command = [name: string, ...args: string[]];
const settings = ["mbsky.com", 443] as const;
const command: Command = ["learn", "TypeScript", "基础"];

可选或rest元组并非一个永远固定长度,length静态类型会相应变化。as const通常产生readonly字面量元组,不是可变的[42,"Hello"],也不冻结运行时数组。需要在运行时阻止修改,要另行设计冻结与对象引用规则。

完整核心源码与验证目标

typescript · 阅读源码
let entry: [number, string, boolean] = [42, "蓝色天空网", true];
console.log(entry[0], entry[1], entry[2]);
entry[0] = 121; const [id, title] = entry;
console.log(id, title);
const extra: [boolean, number] = [false, 100];
const combined: [number, string, boolean, boolean, number] = [...entry, ...extra];
console.log(combined.slice(1).join(","));
const copy = Array.from(entry); copy.push("新项"); console.log(copy.join(","));
const removed = entry.pop(); console.log(removed, entry.length);
TypeScript 元组:蓝色天空网核心实验

所需环境:固定版本的TypeScript编译器,启用strict;编译产物在支持ES2022的JavaScript环境中运行

当前源码 SHA256:b68aaaec02aef00c80e5d601fd89d0b7aaad5c5d1f4654a8dec42c248d34ab1e

下载本站示例源码
预期结果 · 待实际运行核验
  • 初始42、蓝色天空网、true;更新后121、蓝色天空网
  • 连接后slice序列蓝色天空网,true,false,100
  • 普通数组副本121,蓝色天空网,true,新项
  • pop实际移除true,运行时length=2;之后不读取原第三位置

常见错误与边界

  • 裸混合数组通常不是元组
  • 可变元组push/pop可能破坏运行时固定长度假设
  • as const产生静态只读,不是运行时冻结

练习与解答

将核心入参改为readonly元组,再为需修改的函数创建副本,并用类型标注说明副本是否仍保持位置。

展开参考解答

readonly参数不能pop;用展开加显式元组目标可建立可变副本,但要明确是否继续允许变长。普通Array.from副本通常是联合元素数组。

规范与官方依据