typescript/prefer-ts-expect-error Pedantic
它的作用
强制使用 @ts-expect-error 而不是 @ts-ignore。
为什么这不好?
TypeScript 允许你通过在出错行之前紧接着放置一个以 @ts-ignore 或 @ts-expect-error 开头的注释来抑制该行上的所有错误。 这两个指令的作用相同,除了 @ts-expect-error 如果放在一行本来没有错误的代码前面,会导致类型错误。
这意味着 @ts-ignore 很容易被遗忘,即使它所抑制的错误已经修复,它也可能仍然留在代码中。 这很危险,因为如果该行后来出现新的错误,它会被被遗忘的 @ts-ignore 抑制,从而被漏掉。
示例
以下是此规则的错误代码示例:
ts
// @ts-ignore
const str: string = 1;
/**
* 解释性注释
*
* @ts-ignore */
const multiLine: number = "value";以下是此规则的正确代码示例:
ts
/**
* 解释性注释
*
* @ts-expect-error */
const multiLine: number = "value";如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"typescript/prefer-ts-expect-error": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"typescript/prefer-ts-expect-error": "error",
},
});bash
oxlint --deny typescript/prefer-ts-expect-error版本
此规则于 v0.2.11 中添加。
