TypeScript 5.6 新特性:用 Disjointed Union 让类型更安全
TypeScript 5.6 New Features: Safer Types with Disjointed Unions
| Kevin Liu | 2026-08-13T16:12:28
TypeScript 5.6 带来了 Disjointed Union 等多个新特性,这篇用实例讲解最实用的几个改进。
Practical examples of TypeScript 5.6's most useful new features including Disjointed Unions and iterator helpers.
## TypeScript 5.6 来了 TS 5.6 在 2026 年 Q3 发布,带来了不少类型系统的改进。挑几个最实用的聊一下。 ## 1. Disjointed Union Types 这是最大的新特性。之前用联合类型做类型收窄时,经常遇到这种情况: ```typescript // 之前 type Shape = | { kind: 'circle'; radius: number } | { kind: 'rectangle'; width: number; height: number }; function area(shape: Shape): number { if (shape.kind === 'circle') { return Math.PI * shape.radius ** 2; } else { return shape.width * shape.height; // TS 能推断出是 rectangle } } ``` 这没问题。但如果联合类型没有共同的判别字段呢? ```typescript // 之前——类型收窄失败 type Response = { data: string } | { error: Error }; function handle(res: Response) { if ('data' in res) { console.log(res.data); // OK } else { console.log(res.error); // 之前 TS 不能确定这里一定有 error! } } ``` 5.6 的 Disjointed Union 解决了这个问题。当 TS 检测到联合类型的各分支属性**不重叠**时,会自动做精确的类型收窄。 ```typescript // 5.6——自动识别 disjoint 属性 function handle(res: Response) { if ('data' in res) { console.log(res.data); // 类型是 { data: string } } else { console.log(res.error); // 类型是 { error: Error } ✓ } } ``` ## 2. Iterator Helpers 终于不用 `Array.from()` 了! ```typescript // 之前 const result = Array.from(map.values()) .filter(x => x > 10) .map(x => x * 2); // 5.6——直接在 iterator 上链式操作 const result = map.values() .filter(x => x > 10) .map(x => x * 2) .toArray(); ``` 支持的方法:`map`、`filter`、`take`、`drop`、`flatMap`、`reduce`、`forEach`、`some`、`every`、`find`、`toArray`。 好处是**惰性求值**——`filter` 和 `map` 不会创建中间数组,在处理大数据集时内存效率更高。 ## 3. `satisfies` 的增强 ```typescript // 5.6 支持 satisfies 在更多位置使用 const config = { port: 3000, host: 'localhost', debug: true, } satisfies Record; // 类型保留了字面量类型 config.port; // 类型是 3000,不是 number ``` ## 4. `using` 声明 类似 C# 的 `using` 和 Python 的 `with`,自动资源管理: ```typescript function readFile() { using file = openFile('data.txt'); // file 在作用域结束时自动关闭(调用 Symbol.dispose) return file.readAll(); } // file 自动关闭 // 异步版本 async function fetchData() { await using conn = await getConnection(); return conn.query('SELECT * FROM users'); } // conn 自动关闭 ``` 这对数据库连接、文件句柄、锁等需要手动释放的资源特别有用。 ## 5. 正则表达式命名捕获组类型 ```typescript // 5.6 能推断正则的命名捕获组类型 const pattern = /(?\d{4})-(?\d{2})-(?\d{2})/; const match = '2026-09-08'.match(pattern); if (match?.groups) { match.groups.year; // 类型是 string ✓ match.groups.month; // 类型是 string ✓ match.groups.foo; // 编译错误!不存在的捕获组 ✓ } ``` ## 升级建议 TS 5.6 是一个稳健的更新,没有 Breaking Changes。建议直接升级: ```bash npm install -D typescript@5.6 ``` Disjointed Union 和 Iterator Helpers 是最值得使用的两个特性,能让代码更简洁和类型更安全。
TypeScript 5.6 key features: Disjointed Unions for automatic type narrowing without discriminant fields, Iterator Helpers for lazy chainable operations on iterators, enhanced satisfies preserving literal types, using declarations for automatic resource management (like C# using/Python with), and regex named capture group type inference.