Skip to content

我们很高兴地宣布 Oxlint 推出了类型感知 linting 的 alpha 版本!

🌐 We're excited to announce the alpha release of type-aware linting in Oxlint!

概览

🌐 Overview

继我们在八月的技术预览之后,我们很高兴地宣布类型感知的代码检查已达到 Alpha 版本。这一里程碑在稳定性、可配置性和规则覆盖方面带来了显著的改进。

🌐 Following our technical preview in August, we're excited to announce that type-aware linting has reached alpha status. This milestone brings significant improvements in stability, configurability, and rule coverage.

类型感知的代码检查使得 no-floating-promisesno-misused-promisesawait-thenable 等强大规则得以实现,它们通过利用 TypeScript 的类型系统来捕获错误。现在已有 43 条类型感知规则可用,你可以在运行时错误发生之前捕获整个类别的错误。

🌐 Type-aware linting enables powerful rules like no-floating-promises, no-misused-promises, and await-thenable that catch bugs by utilizing TypeScript's type system. With 43 type-aware rules now available, you can catch entire categories of runtime errors before they happen.

在本文中:

快速开始

🌐 Quick Start

安装 oxlintoxlint-tsgolint,然后使用 --type-aware 参数运行:

🌐 Install oxlint and oxlint-tsgolint, then run with the --type-aware flag:

sh
npm add -D oxlint oxlint-tsgolint@latest
npx oxlint --type-aware
sh
pnpm add -D oxlint oxlint-tsgolint@latest
pnpm oxlint --type-aware
sh
yarn add -D oxlint oxlint-tsgolint@latest
yarn oxlint --type-aware
sh
bun add -D oxlint oxlint-tsgolint@latest
bunx oxlint --type-aware

要尝试特定的类型感知规则而不进行其他配置(oxlint-tsgolint 必须已经全局或本地安装):

🌐 To try a specific type-aware rule without other configuration (oxlint-tsgolint must be installed globally or locally already):

sh
npx oxlint --type-aware -A all -D typescript/no-floating-promises
sh
pnpx oxlint --type-aware -A all -D typescript/no-floating-promises
sh
yarn oxlint --type-aware -A all -D typescript/no-floating-promises
sh
bunx oxlint --type-aware -A all -D typescript/no-floating-promises

有关更多配置选项,请参阅我们的使用指南

🌐 For more configuration options, see our usage guide.

性能

🌐 Performance

项目Oxlint + 类型感知ESLint + typescript-eslint提升
vuejs/core2.531 秒20.800 秒8.22 倍
outline/outline4.448 秒55.070 秒12.38 倍

基准测试在一台配备 M2 Max 芯片的 MacBook Pro 上进行,该芯片拥有 12 核(8 个高性能核和 4 个高效核)。

🌐 Benchmarks were performed on a MacBook Pro M2 Max 12 Cores (8 performance and 4 efficiency).

我们的性能测试显示,使用类型感知 lint 的 oxlint 比搭配 typescript-eslinteslint 快大约 10 倍。查看更多详情,请查阅我们的 性能基准

🌐 Our performance testing shows that oxlint with type-aware linting is around 10 times faster than eslint with typescript-eslint. Take a look at our performance benchmarks for more details.

Oxlint 也可以在进行代码检查时对代码库进行类型检查。这避免了重复工作,因为在类型感知的代码检查过程中,大部分类型信息已经被计算出来。

🌐 Oxlint can also be used to typecheck your codebase while linting. This avoids a duplicate work, as much of the type information is already computed during type-aware linting.

已知问题

虽然“tsgolint”已准备好在生产环境中测试,但在处理非常大的代码库时,你可能会遇到内存不足的问题。我们正在优化下一个里程碑的内存使用。我们非常希望你尝试“tsgolint”,并在['tsgolint'仓库](https://github.com/oxc-project/tsgolint)中报告任何内存不足问题,并附上项目细节,帮助我们改善内存使用。

自技术预览以来有什么新变化?

🌐 What's new since the technical preview?

在进行代码风格检查时支持类型检查

🌐 Support for type-checking while linting

tsgolint 现在支持在进行代码检查时从 TypeScript 输出类型检查错误。由于类型感知规则已经需要检查文件中的所有类型,我们能够使用现有的类型信息,而不是丢弃它。这意味着在某些情况下,完全可以跳过单独的类型检查命令(例如 tsc --noEmit),从而减少在 CI 中进行代码检查和类型检查的总时间。

这是一个实验性功能,但你可以通过在 oxlint 命令中添加 --type-check--type-aware 标志来启用它:

🌐 This is an experimental feature, but you can enable it by adding the --type-check and --type-aware flag to the oxlint command:

$ oxlint --type-aware --type-check

  × typescript(TS2322): Type 'number' is not assignable to type 'string'.
   ╭─[index.ts:1:7]
 1 │ const message: string = 1
   ·       ───────
   ╰────

oxlint 中的规则配置支持

🌐 Rule configuration support in oxlint

可以在 oxlint 中配置在 tsgolint 中运行的类型感知规则,就像配置其他 lint 规则一样。例如,你可以配置 no-floating-promises 规则以允许某些安全调用或忽略 void

🌐 Type-aware rules that run in tsgolint can be configured in oxlint just like any other lint rule. For example, you can configure the no-floating-promises rule to allow certain safe calls or ignore void:

json
{
  "rules": {
    "typescript/no-floating-promises": [
      "error",
      {
        "ignoreVoid": true,
        "allowForKnownSafePromises": [
          { "from": "file", "name": "SafePromise" },
          { "from": "lib", "name": "PromiseLike" }
        ]
      }
    ]
  }
}

配置选项与 typescript-eslint 支持的内容保持一致,相关文档可以在每条规则的配置部分找到(例如 no-floating-promises)。

🌐 The configuration options are aligned with what typescript-eslint supports and documentation can be found in the configuration section for each rule (like no-floating-promises).

oxlint 中的行内禁用注释支持

🌐 Inline disable comment support in oxlint

现在,可以像禁用其他 oxlint 规则一样,通过在文件中或某一行添加注释来禁用在 tsgolint 中运行的规则:

🌐 Rules that run in tsgolint can now be disabled similar to any other oxlint rule by placing a comment in the file or on a line:

ts
/* oxlint-disable typescript/no-floating-promises */

// oxlint-disable-next-line typescript/no-floating-promises
[1, 2, 3].map(async (x) => x + 1);

更多支持的规则

🌐 More supported rules

我们继续在将 typescript-eslint 的流行规则移植过来方面取得进展,现在你可以通过 oxlint 使用这些规则。tsgolintoxlint 结合,目前支持 43 条类型感知规则。

🌐 We've continued to make progress on porting popular rules from typescript-eslint which you can now use via oxlint. tsgolint, combined with oxlint, currently supports 43 type-aware rules.

自首次预览以来,还增加了对以下规则的支持:

🌐 Since the initial preview, support for the following rules has also been added:

TypeScript 程序诊断现已报告

🌐 TypeScript program diagnostics are now reported

以前,如果 TypeScript 无法创建和解析程序,这些错误不会被报告,导致对为什么 linting 不起作用感到困惑。现在,我们会将创建程序时的任何问题作为诊断报告,包括 tsconfig.json 文件中的配置问题。

🌐 Previously, if TypeScript failed to create and parse a program, these errors were not reported which led to confusion around why linting was not working. Now, we report any issues with creating a program as a diagnostic, including configuration issues in tsconfig.json files.

例如,如果一个 tsconfig.json 文件包含 baseUrl,这将被报告为错误,因为 baseUrl 已在 TypeScript v7.0 中被移除:

🌐 For example, if a tsconfig.json file contains baseUrl, that will be reported as an error, since baseUrl has been removed from TypeScript in v7.0:

$ oxlint --type-aware

  × typescript(tsconfig-error): Invalid tsconfig
   ╭─[tsconfig.json:4:3]
 3 │     "compilerOptions": {
 4 │         "baseUrl": ".",
   ·         ─────────
 5 │         "experimentalDecorators": true,
   ╰────
  help: Option 'baseUrl' has been removed. Please remove it from your configuration.
        See https://github.com/oxc-project/tsgolint/issues/351 for more information.

针对类型感知规则的自动修复

🌐 Automatic fixes for type-aware rules

类型感知规则现在支持通过 --fix 标志进行自动修复。当你运行 oxlint --type-aware --fix 时,来自 tsgolint 的可修复诊断将像常规 oxlint 修复一样被应用。这使修复工作流程与非类型感知规则完全一致。

🌐 Type-aware rules now support automatic fixes via the --fix flag. When you run oxlint --type-aware --fix, fixable diagnostics from tsgolint are applied just like regular oxlint fixes. This brings full parity with non-type-aware rules for the fix workflow.

技术细节

🌐 Technical details

架构

🌐 Architecture

Oxlint 中的类型感知 lint 使用独特的双二进制架构:

🌐 Type-aware linting in Oxlint uses a unique two-binary architecture:

oxlint CLI (Rust)
  ├─ Handles file traversal, ignore logic, and diagnostics
  ├─ Runs non-type-aware rules and custom JS plugins
  ├─ Passes paths and configuration to tsgolint
  └─ Formats and displays results

tsgolint (Go)
  ├─ Uses typescript-go directly for type checking
  ├─ Executes type-aware rules
  └─ Returns structured diagnostics

这个设计保持了 Oxlint 核心的高性能,同时通过 typescript-go 利用 TypeScript 的类型系统。前后端分离意味着 oxlint 控制用户体验,而 tsgolint 处理类型分析的繁重工作。

🌐 This design keeps Oxlint's core fast while leveraging TypeScript's type system through typescript-go. The frontend-backend separation means oxlint controls the user experience while tsgolint handles the heavy lifting of type analysis.

TypeScript 兼容性

🌐 TypeScript compatibility

tsgolint 基于 typescript-go,这是微软的 Go 语言重写版本,将成为 TypeScript 7.0。有关 TypeScript 7 最新进展的更多详情,请参阅 官方 TypeScript 博客文章。这意味着你可能会遇到一些不再被支持的功能。

重要的兼容性说明:

  • 仅支持 TypeScript 7.0 及以上版本的功能
  • 不支持 7.0 之前的语法和已弃用的功能
  • 在 TypeScript 7.0 中,像 baseUrl 这样的旧 tsconfig.json 选项已被移除

如果你正在使用 TypeScript 6.0 或更早版本的已弃用功能,你需要先迁移你的代码库。请参阅 TypeScript 迁移指南 获取有关更新已弃用 tsconfig 选项的帮助。

🌐 If you're using deprecated features from TypeScript 6.0 or earlier, you'll need to migrate your codebase first. See the TypeScript migration guide for help updating deprecated tsconfig options.

实现细节

🌐 Implementation details

tsgolint 不使用 typescript-go 的公共 API。相反,它通过填充内部 API 来使其可访问,从而编译 typescript-go。我们会积极跟踪 typescript-go 的更新,并在必要时修复破坏性更改。

我们的 typescript-go 分支会定期使用 renovatebot 同步,确保我们保持与最新改进和修复同步。一旦 TypeScript 7.0 正式发布,我们将跟踪稳定版本,而不是主分支的最新版本。

🌐 Our typescript-go fork is synced regularly using renovatebot, ensuring we stay current with the latest improvements and fixes. Once TypeScript 7.0 is officially released, we will track stable releases rather than the tip of the main branch.

接下来是什么

🌐 What's next

我们正在积极推进以下 beta 版本的改进:

🌐 We're actively working on the following improvements for the beta release:

  • 更多支持的规则 - 目前我们支持 typescript-eslint 中 59 条类型感知规则中的 43 条。随着我们迈向测试版发布,我们计划继续扩大规则覆盖范围。
  • 性能和内存使用改进 - 我们将继续优化性能,特别是针对非常大的单体仓库。

致谢

🌐 Acknowledgements

我们想向以下人员表示感谢:

🌐 We'd like to extend our gratitude to:

  • TypeScript 团队负责创建 typescript-go
  • 感谢“typescript-eslint”团队的温馨支持。
  • @auvred 创建了 tsgolint
  • @camchenry 负责持续的性能工作,以及实现规则选项支持。

试试

🌐 Try it out

准备好开始了吗?请前往上方的 快速入门 部分来安装并运行类型感知检查工具。

🌐 Ready to get started? Head to the Quick Start section above to install and run type-aware linting.

我们很想听取你对类型感知型代码检查的反馈,并期待看到它如何帮助改善你的开发工作流程。

🌐 We'd love to hear your feedback on type-aware linting and are excited to see how it helps improve your development workflow.

联系我们:

🌐 Connect with us:

  • Discord:加入我们的社区服务器进行实时讨论
  • GitHub:在 GitHub 讨论 上分享反馈
  • 问题:将 oxlint 的错误报告给 oxc,将类型感知的 lint 错误报告给 tsgolint