为什么 // @ts-expect-error 不影响 typescript-eslint 的 lint 结果?
为什么 // ts-expect-error 不影响 typescript-eslint 的 lint 结果【免费下载链接】typescript-eslint:sparkles: Monorepo for all the tooling which enables ESLint to support TypeScript项目地址: https://gitcode.com/GitHub_Trending/ty/typescript-eslint在 TypeScript 项目里用 typescript-eslint 做静态检查时一个常见的困惑是明明在出错行上方加了// ts-expect-error或// ts-ignore压掉了编译错误为什么 ESLint 的报错还在反过来/* eslint-disable */注释也压不掉 TypeScript 的类型错误。这篇文档回答这个问题并给出一条可执行的验证路径按 Quickstart 搭好最小项目跑一次npx eslint .观察两类注释各自的行为再按需启用typescript-eslint/ban-ts-comment规则来约束ts-...注释的写法。两类注释为什么互不影响TypeScript FAQs 对这一点有明确解释// ts-expect-error和// ts-ignore是 TypeScript 的注释指令只影响 TypeScript 的类型检查TypeScript 是类型检查器ESLint 是 linter两者是相互独立的工具typescript-eslint 不会去复现或转译 TypeScript 的报错反过来同理ESLint 的配置注释/* eslint ... */只影响 ESLint不改变 TypeScript 的类型检查结果。也就是说// ts-expect-error让 TypeScript 编译器不再报告下一行的类型错误但 ESLint 的规则引擎并不消费这条指令所以 lint 输出保持原样。这是设计使然不是配置问题——文档中没有、也不需要任何配置能让ts-expect-error顺带关闭 ESLint 规则。搭好最小项目并复现行为以下配置和命令均来自 Quickstart使用 ESLint 的 flat config 格式。1. 安装依赖在项目根目录执行npm install --save-dev eslint eslint/js typescript typescript-eslint2. 创建eslint.config.mjs// eslint.config.mjs // ts-check import js from eslint/js; import { defineConfig } from eslint/config; import tseslint from typescript-eslint; export default defineConfig({ files: [**/*.{js,ts}], extends: [js.configs.recommended, tseslint.configs.recommended], });tseslint.configs.recommended开启了 typescript-eslint 的推荐规则集其中已经以error级别启用了typescript-eslint/ban-ts-comment见 configs/flat/recommended.ts。3. 写一个验证文件内容取自 ban-ts-comment 规则文档的示例// sample.ts if (false) { // ts-ignore: Unreachable code error console.log(hello); }4. 在项目根目录运行npx eslint .预期观察到的行为报告文案来自 规则实现中的消息定义// ts-ignore会被报告为Use ts-expect-error instead of ts-ignore, as ts-ignore will do nothing if the following line is error-free.并附带一个将注释替换为// ts-expect-error的 suggestion把注释改成不带描述信息的// ts-expect-error后报告变为Include a description after the ts-expect-error directive to explain why the ts-expect-error is necessary. The description must be 3 characters or longer.默认minimumDescriptionLength为 3再补上至少 3 个字符的描述例如// ts-expect-error here is why the error is expected该注释就不再被ban-ts-comment报告这是规则文档给出的合法示例。这里可以看到两条独立的事实线ts-...注释是否被报告完全由 ESLint 规则决定而它是否影响类型错误只由 TypeScript 编译器决定两者互不干预。用 ban-ts-comment 约束 ts-... 注释的写法如果你想更严格地治理代码库里的ts-...注释可以配置typescript-eslint/ban-ts-comment规则。该规则支持 TypeScript 提供的四个注释指令// ts-expect-error、// ts-ignore、// ts-nocheck、// ts-check。默认行为不传选项时ts-check被允许因为它开启而非抑制错误ts-expect-error在带描述时允许ts-ignore和ts-nocheck一律报告。每个指令选项可取值true、allow-with-description或{ descriptionFormat: string }另有全局选项minimumDescriptionLength。示例均来自规则文档将其作为typescript-eslint/ban-ts-comment规则的选项设置即可完全禁止ts-ignoretrue表示发现该指令即报告{ ts-ignore: true }要求描述信息无描述的指令被报告{ ts-expect-error: allow-with-description }描述信息必须符合正则格式{ ts-expect-error: { descriptionFormat: ^: TS\\d because .$ } }配合minimumDescriptionLength限制描述最短长度例如{ ts-expect-error: allow-with-description, minimumDescriptionLength: 10 }——此时// ts-expect-error: TODO会被报告而文档示例中// ts-expect-error The rationale for this override is described in issue #1337 on GitLab这类长描述则合法。typescript-eslint 的strict共享配置就是对这条规则传了{ minimumDescriptionLength: 10 }见 configs/flat/strict.ts。注意descriptionFormat只检查格式、不检查长度minimumDescriptionLength只检查长度两者同时配置时先查长度再查格式任一不满足都会被报告。边界与限制ban-ts-comment不改变 lint 结果。它只报告ts-...注释本身是否使用、是否带描述、描述格式不能也不打算让ts-expect-error去关闭 ESLint 规则。需要为单行关闭 ESLint 规则时应使用 ESLint 自己的配置注释/* eslint-disable-next-line ... */一类正如 FAQ 所说这类注释只影响 ESLint。ts-expect-error与ts-ignore的行为差异两者都能压掉下一行的类型错误但ts-expect-error放在本来没有报错的行上时自身会产生一个类型错误因此被遗忘的ts-ignore不会像ts-expect-error那样自我暴露。这也是该规则对ts-ignore提供替换 suggestion、且 prefer-ts-expect-error 规则已被废弃、由ban-ts-comment取代的原因。规则自身的盲区注释内容只是文本匹配规则对块注释只检查最后一行中的指令见 规则实现 的匹配逻辑例如描述写在前面的多行块注释不会被当作指令。完成上述验证后你对这套行为就有了确定性的判断依据// ts-expect-error管的是类型检查器ESLint 输出不受它影响要用 lint 手段管理这些注释就用ban-ts-comment并按需收紧选项。【免费下载链接】typescript-eslint:sparkles: Monorepo for all the tooling which enables ESLint to support TypeScript项目地址: https://gitcode.com/GitHub_Trending/ty/typescript-eslint创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考