prefer-ts-expect-error
Enforce using
@ts-expect-error
over@ts-ignore
.
此规则报告的一些问题可通过 --fix
ESLint 命令行选项自动修复。
此规则已被弃用,以支持 @typescript-eslint/ban-ts-comment
。此规则(@typescript-eslint/prefer-ts-expect-error
)将在 typescript-eslint 的未来主要版本中删除。
¥This rule has been deprecated in favor of @typescript-eslint/ban-ts-comment
.
This rule (@typescript-eslint/prefer-ts-expect-error
) will be removed in a future major version of typescript-eslint.
当它首次创建时,@typescript-eslint/ban-ts-comment
规则仅负责建议删除 @ts-ignore
指令。它后来更新为建议用 @ts-expect-error
指令替换 @ts-ignore
,以便它完全替换 @typescript-eslint/prefer-ts-expect-error
。
¥When it was first created, @typescript-eslint/ban-ts-comment
rule was only responsible for suggesting to remove @ts-ignore
directive.
It was later updated to suggest replacing @ts-ignore
with @ts-expect-error
directive, so that it replaces @typescript-eslint/prefer-ts-expect-error
entirely.
TypeScript 允许你通过在错误行之前立即放置以 @ts-ignore
或 @ts-expect-error
开头的注释来抑制行上的所有错误。这两个指令的作用相同,只是如果将 @ts-expect-error
放在没有错误的行之前,会导致类型错误。
¥TypeScript allows you to suppress all errors on a line by placing a comment starting with @ts-ignore
or @ts-expect-error
immediately before the erroring line.
The two directives work the same, except @ts-expect-error
causes a type error if placed before a line that's not erroring in the first place.
这意味着 @ts-ignore
很容易被遗忘,并且即使在他们抑制的错误被修复后仍保留在代码中。这很危险,因为如果该行出现新错误,它将被遗忘的 @ts-ignore
抑制,因此会被忽略。
¥This means it's easy for @ts-ignore
s to be forgotten about, and remain in code even after the error they were suppressing is fixed.
This is dangerous, as if a new error arises on that line it'll be suppressed by the forgotten about @ts-ignore
, and so be missed.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/prefer-ts-expect-error": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/prefer-ts-expect-error": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
此规则报告 @ts-ignore
和 @ts-expect-error
具有相同名称但 的类型不能分配给 的情况。
¥This rule reports any usage of @ts-ignore
, including a fixer to replace with @ts-expect-error
.
- ❌ Incorrect
- ✅ Correct
// @ts-ignore
const str: string = 1;
/**
* Explaining comment
* * @ts-ignore */
const multiLine: number = 'value';
/** @ts-ignore */
const block: string = 1;
const isOptionEnabled = (key: string): boolean => {
// @ts-ignore: if key isn't in globalOptions it'll be undefined which is false
return !!globalOptions[key];
};
Open in Playground// @ts-expect-error
const str: string = 1;
/**
* Explaining comment
* * @ts-expect-error */
const multiLine: number = 'value';
/** @ts-expect-error */
const block: string = 1;
const isOptionEnabled = (key: string): boolean => {
// @ts-expect-error: if key isn't in globalOptions it'll be undefined which is false
return !!globalOptions[key];
};
Open in Playground选项
该规则不可配置。
何时不使用它
¥When Not To Use It
如果你针对多个版本的 TypeScript 进行编译并使用 @ts-ignore
忽略特定于版本的类型错误,则此规则可能会妨碍你。你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。
¥If you are compiling against multiple versions of TypeScript and using @ts-ignore
to ignore version-specific type errors, this rule might get in your way.
You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
进一步阅读
¥Further Reading
'## 资源'