TS 5.5 ~ 5.8 新特性
约 461 字大约 2 分钟
布欧-Lewyon
2026-05-16
首页 › TypeScript › TS 5.x 新特性 › TS 5.5 ~ 5.8
TS 5.5
推断类型守卫
// TS 5.5 能自动推断函数是否是类型守卫
function isNumberArray(value: unknown) {
return Array.isArray(value) && value.every(v => typeof v === 'number');
}
function process(value: unknown) {
if (isNumberArray(value)) {
value; // ✅ 5.5 中自动收窄为 number[]
}
}
// 之前版本需要手动写 `value is number[]` 类型谓词--isolatedDeclarations
// tsconfig.json
{
"compilerOptions": {
"isolatedDeclarations": true
}
}
// 每个源文件可独立生成 .d.ts 文件,
// 不需要先进行整体类型检查,大幅提升大型项目的构建速度控制流窄化改进
// 闭包内的类型窄化更加智能
function process(items: (string | number)[]) {
const filtered = items.filter(x => typeof x === 'string');
filtered; // ✅ 5.5 中推断为 string[]
}TS 5.6
迭代替换改进
// 语法错误的迭代替换提示更加友好
// 对 Iterator / Generator 的类型检查增强--noUncheckedSideEffectImports
// tsconfig.json
{
"compilerOptions": {
"noUncheckedSideEffectImports": true
}
}
// 检查是否有未使用的副作用导入TS 5.7
类型系统完善
// never 类型在交叉类型中的行为改进
// 更精确的索引访问类型推导TS 5.8
--erasableSyntaxOnly
// tsconfig.json
{
"compilerOptions": {
"erasableSyntaxOnly": true
}
}
// 只允许"可擦除"的类型语法(编译后完全移除的语法,无运行时影响)
// 例如:禁止 enum(有运行时行为)、禁止 namespace(非声明文件)
// 推荐在需要干净输出的库项目中使用TS 5.x 新特性总览
| 版本 | 发布日期 | 关键特性 |
|---|---|---|
| 5.0 | 2023.03 | 标准装饰器、const 类型参数、bundler 解析 |
| 5.1 | 2023.06 | in 守卫改进、@link JSDoc |
| 5.2 | 2023.08 | using 声明、装饰器元数据 |
| 5.3 | 2023.11 | importAttributes、satisfies 完善 |
| 5.4 | 2024.03 | NoInfer、groupBy 类型 |
| 5.5 | 2024.06 | 推断类型守卫、isolatedDeclarations |
| 5.6 | 2024.09 | 迭代替换改进 |
| 5.7 | 2025.01 | 类型系统完善 |
| 5.8 | 2026.02 | erasableSyntaxOnly、性能改进 |
