TypeScript

命令行工具

microsoft/TypeScript

TypeScript 是 JavaScript 的类型超集,编译为干净的 JavaScript 代码。

项目简介

TypeScript 为 JavaScript 添加可选的静态类型,支持大型应用的更好的工具和错误检测。它编译为可读的、基于标准的 JavaScript,可在任何浏览器、主机或操作系统上运行。被数百万开发者用于构建企业级软件。

README 预览

# TypeScript\r\n\r\n\r\n\r\n[](https://github.com/microsoft/TypeScript/actions/workflows/ci.yml)\r\n[](https://www.npmjs.com/package/typescript)\r\n[](https://www.npmjs.com/package/typescript)\r\n[](https://securityscorecards.dev/viewer/?uri=github.com/microsoft/TypeScript)\r\n\r\n\r\n[TypeScript](https://www.typescriptlang.org/) is a language for application-scale JavaScript. TypeScript adds optional types to JavaScript that support tools for large-scale JavaScript applications for any browser, for any host, on any OS. TypeScript compiles to readable, standards-based JavaScript. Try it out at the [playground](https://www.typescriptlang.org/play/), and stay up to date via [our blog](https://blogs.msdn.microsoft.com/typescript) and [Twitter account](https://twitter.com/typescript).\r\n\r\nFind others who are using TypeScript at [our community page](https://www.typescriptlang.org/community/).\r\n\r\n## Installing\r\n\r\nFor the latest stable version:\r\n\r\n```bash\r\nnpm install -D typescript\r\n```\r\n\r\nFor our nightly builds:\r\n\r\n```bash\r\nnpm install -D typescript@next\r\n```\r\n\r\n## Contribute\r\n\r\n**NOTE: Code changes in this repo are now limited to a small category of fixes**:\r\n\r\n * Crashes that were introduced in 5.9 or 6.0 that *also* repro in 7.0 *and* have a portable fix *and* don't incur other behavioral changes\r\n * Security issues\r\n * Language service crashes that substantially impact mainline usage\r\n * Serious regressions from 5.9 (these must *seriously* impact a *large* proportion of users)\r\n\r\nMost bug fixes should be submitted to the [typescript-go](https://github.com/microsoft/TypeScript-go) repository.\r\nFeature additions and behavioral changes are currently on pause until TypeScript 7.0 is completed.\r\n\r\nThere are many ways to [contribute](https://github.com/microsoft/TypeScript/blob/main/CONTRIBUTING.md) to TypeScript.\r\n* [Submit bugs](https://github.com/microsoft/TypeScript/issues) and help us verify fixes as they are

常见问题 (5)

故障排除
为什么在更改 node_modules 中的 .d.ts 文件后,tsc --incremental 会漏报错误(诊断结果过时)?

这是 TypeScript 7.0.2 中的一个回归问题(在 6.0.3 中工作正常):当某个包的模块声明文件中的 declare global 块发生更改时,由于 .tsbuildinfo 缓存未被失效,增量构建会保留过时的诊断信息。增量运行不报告任何错误,而全新构建则会报告这些错误(例如 TS2345)。

修复方法与变通方案:
1. 升级到 TypeScript 7.1+ —— 该 bug 已确认在 7.1 nightly 中修复(npm i typescript@next)。
2. 如果受限于 7.0.x,请在每次运行前删除构建信息文件,以强制进行完整的重新检查:rm -f <path>.tsbuildinfo && tsc --noEmit --incremental --tsBuildInfoFile <path>.tsbuildinfo -p tsconfig.json。
3. 或者,通过一次干净的非增量运行来验证类型:tsc --noEmit -p tsconfig.json。

注意:此问题通常会影响类型测试配置(例如 Vitest/expect-webdriverio),这些配置会针对 node_modules 中包的全局类型增强运行 tsc --noEmit --incremental。

来源 Issue #64715
故障排除
为什么当全局声明文件发生更改时,TypeScript 7.0.2 的增量构建会生成过时的 .d.ts 文件?

这是 TypeScript 7.0.2 中的一个已知 bug。在启用 isolatedModules + declaration + incremental 的情况下,当全局作用域文件(例如 declare global 或 @types/node)与源文件在同一次构建中发生变更时,依赖文件的 .d.ts 不会被重新生成,且 .tsbuildinfo 会错误地将它们记录为最新状态,因此过时的输出会在多次构建之间持续存在。

该修复(microsoft/typescript-go#4665)不会回溯到 7.0.x —— 团队只回溯关键的安全/稳定性修复。解决方案:

1. 升级到 TypeScript 7.1(或 nightly 版本 typescript@7.1.0-dev.20261011.1 及更高版本):npm install typescript@next。
2. 在 7.0.2 上的变通方案:每当依赖升级或修改涉及全局声明时,删除 .tsbuildinfo 文件(或执行 clean build),强制完整重新生成所有声明文件。

来源 Issue #64809
故障排除
如何修复在 TypeScript 7.0 native (tsgo) 中使用 `Exclude<keyof T & string, ...>` 索引 `as` 重映射的映射类型时出现的 TS2536 错误?

这是原生 TypeScript 7.0.2 编译器(tsgo)中的一个回归问题;同样的代码在 TS 5.9.3 和 6.0.3 中可以正常编译通过。变通方法:用映射类型自身的 keyof 进行索引,而不是用 Exclude 重新计算重映射后的键集合。将映射类型提取为一个别名,并使用 Map<T>[keyof Map<T>]:

type EntryPathMap<T> = {
  [K in keyof T & string as K extends '_key' ? never : K]: [PathTuples<T[K]>] extends [never]
    ? never
    : readonly [K, ...ChildPaths<T[K]>];
};
type PathTuples<T> = T extends object ? EntryPathMap<T>[keyof EntryPathMap<T>] : [];

keyof EntryPathMap<T> 解析出的键集合与 Exclude<keyof T & string, '_key'> 相同,并且所有编译器都接受这种写法。或者,在原生编译器的回归问题修复之前,将 TypeScript 锁定为 5.9.3 或 6.0.3(@typescript/typescript6)。

来源 Issue #64768
故障排除
为什么 tsgo(TypeScript 原生 LSP 服务器)在关闭时崩溃并报错 'panic: assignment to entry in nil map'?

这是 typescript-go LSP 文件监视器(lspwatcher.WatchFiles)中的一个竞态条件。监视器在安装订阅时会释放其锁;如果 Watcher.Close 在此期间运行,它会将 w.watches 设为 nil,随后的 map 赋值就会引发 panic。典型的触发场景是:客户端在关闭文档并退出的同时,后台的项目快照更新正在重新注册监视——即客户端未声明动态 workspace/didChangeWatchedFiles 注册能力、服务器使用其内部监视器的情况。tsgo --lsp --stdio 会以退出码 2 退出。

**修复方法:** 将你的 TypeScript native preview(typescript@7.x / tsgo)升级到包含 microsoft/typescript-go#4845 修复的构建版本。该修复在发布订阅之前于锁内重新检查 closed,并返回 lspwatcher: closed 错误,而不是 panic。

**在能够升级之前的临时解决方案:**
1. 让你的 LSP 客户端声明动态文件监视注册(workspace.didChangeWatchedFiles.dynamicRegistration: true),这样服务器会使用由客户端管理的监视器,而不是包含该竞态的内部监视器。
2. 如果从源码构建,请修补 internal/lsp/lspwatcher/lspwatcher.go(WatchFiles):在重新获取 w.mu 之后,重新检查 w.closed;如果已关闭,则关闭尚未发布的订阅并返回现有的 lspwatcher: closed 错误。不要抑制 panic,也不要禁用监视器。

来源 Issue #64810
故障排除
如何修复使用 TypeScript 5.0 装饰器时出现的 "Cannot find name 'ClassMethodDecoratorFunction'" 错误?

这是 TypeScript 5.0.4 的 lib.decorators.d.ts 中的一个文档 bug:示例中引用了 ClassMethodDecoratorFunction,但该类型从未随 lib 文件一起发布(示例中还包含无效的函数语法)。变通方法:在你的项目中自行声明该类型:

type ClassMethodDecoratorFunction<
  This = unknown,
  Value extends (this: This, ...args: any[]) => any = (this: This, ...args: any[]) => any
> = (
  value: Value,
  context: ClassMethodDecoratorContext<This, Value>
) => Value | void;

这样绑定方法的示例就可以正常工作了:

const bound: ClassMethodDecoratorFunction = function (value, context) {
  if (context.private) throw new TypeError("Not supported on private methods.");
  context.addInitializer(function (this: any) {
    this[context.name] = this[context.name].bind(this);
  });
};

注意:请使用普通的 function 表达式(官方示例中的简写形式是无效语法),并将 this 断言为 any(或使用映射访问),以避免 this[context.name] 出现隐式 any 错误。同时也建议升级到更新的 TypeScript 版本,因为 lib.decorators.d.ts 中的错误示例已在 5.0.x 之后得到修正。社区中也有现成的类型可用,位于 ajvincent/es-membrane(ClassMethodDecoratorFunction.d.mts)。

来源 Issue #54099