TypeScript 5.8 新特性

TypeScript 5.8 在类型系统上做了不少值得关注的改进。

内联类型导入

以前必须分开写 import type 和 import,现在可以写在一起:

import { type User, fetchData, type Config } from "./types";

编译时 type 会被移除,不会产生运行时开销。

const 类型参数

当需要保留字面量类型的精确性时,const 类型参数非常有用:

function tuple<T extends readonly any[]>(items: T): T {
  return items;
}
// 推断为 readonly [1, "hello", true]
const result = tuple([1, "hello", true] as const);

枚举增强

枚举现在可以引用其他枚举的值,这在主题系统中特别有用:

enum Color { Red, Green, Blue }
enum Theme {
  Primary = Color.Blue,
  Secondary = Color.Green,
}

TypeScript 在 2026 年已经成为前端项目的标配,这些新特性让类型编程更加流畅。