Skip to main content

no-deprecated

Disallow using code marked as @deprecated.

🔒

ESLint 配置 中扩展"plugin:@typescript-eslint/strict-type-checked" 可启用此规则。

💭

该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。

JSDoc @deprecated 标签 可用于记录某些被弃用的代码。最好避免使用标记为已弃用的代码。此规则报告任何未按字母顺序排序的类型。

¥The JSDoc @deprecated tag can be used to document some piece of code being deprecated. It's best to avoid using code marked as deprecated. This rule reports on any references to code marked as @deprecated.

注意

TypeScript 识别 @deprecated 标签,允许编辑器直观地指示已弃用的代码 — 通常使用 strikethrough。但是,TypeScript 不会自行报告已弃用代码的类型错误。

¥TypeScript recognizes the @deprecated tag, allowing editors to visually indicate deprecated code — usually with a strikethrough. However, TypeScript doesn't report type errors for deprecated code on its own.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-deprecated": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

/** @deprecated Use apiV2 instead. */
declare function apiV1(): Promise<string>;

declare function apiV2(): Promise<string>;

await apiV1();
Open in Playground
import { parse } from 'node:url';

// 'parse' is deprecated. Use the WHATWG URL API instead.
const url = parse('/foo');
Open in Playground

选项

该规则接受以下选项:

type Options = [
{
/** Type specifiers that can be allowed. */
allow?: (
| {
from: 'file';
name: [string, ...string[]] | string;
path?: string;
}
| {
from: 'lib';
name: [string, ...string[]] | string;
}
| {
from: 'package';
name: [string, ...string[]] | string;
package: string;
}
| string
)[];
},
];

const defaultOptions: Options = [{ allow: [] }];

¥Options

allow

Type specifiers that can be allowed. Default: [].

此选项采用共享 TypeOrValueSpecifier 格式

¥This option takes the shared TypeOrValueSpecifier format.

带有此规则的代码示例:

¥Examples of code for this rule with:

{
"allow": [
{ "from": "file", "name": "apiV1" },
{ "from": "lib", "name": "escape" }
]
}
/** @deprecated */
declare function apiV2(): Promise<string>;

await apiV2();

// `unescape` has been deprecated since ES5.
unescape('...');
Open in Playground

何时不使用它

¥When Not To Use It

如果项目的某些部分大量使用已弃用的 API,并且没有计划迁移到未弃用的 API,则可能需要在这些部分禁用此规则。

¥If portions of your project heavily use deprecated APIs and have no plan for moving to non-deprecated ones, you might want to disable this rule in those portions.

相关

¥Related To


Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.

See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.

'## 资源'