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.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-deprecated": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-deprecated": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ Incorrect
- ✅ Correct
/** @deprecated Use apiV2 instead. */
declare function apiV1(): Promise<string>;
declare function apiV2(): Promise<string>;
await apiV1();
Open in Playgroundimport { parse } from 'node:url';
// 'parse' is deprecated. Use the WHATWG URL API instead.
const url = parse('/foo');
Open in Playground/** @deprecated Use apiV2 instead. */
declare function apiV1(): Promise<string>;
declare function apiV2(): Promise<string>;
await apiV2();
Open in Playground// Modern Node.js API, uses `new URL()`
const url2 = new URL('/foo', 'http://www.example.com');
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" }
]
}
- ❌ Incorrect
- ✅ Correct
/** @deprecated */
declare function apiV2(): Promise<string>;
await apiV2();
// `unescape` has been deprecated since ES5.
unescape('...');
Open in Playgroundimport { Bar } from 'bar-lib';
/** @deprecated */
declare function apiV1(): Promise<string>;
await apiV1();
// `escape` has been deprecated since ES5.
escape('...');
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
-
import/no-deprecated
和import-x/no-deprecated
:不使用类型信息,但也支持 TomDoc¥
import/no-deprecated
andimport-x/no-deprecated
: Does not use type information, but does also support TomDoc -
eslint-plugin-deprecation
(deprecation/deprecation
):此规则的前身在单独的插件中¥
eslint-plugin-deprecation
(deprecation/deprecation
): Predecessor to this rule in a separate plugin
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.
'## 资源'