Skip to content
← 回到规则

typescript/no-deprecated Pedantic

💭 This rule requires type information.

它的作用

🌐 What it does

禁止使用标记为 @deprecated 的代码。

🌐 Disallow using code marked as @deprecated.

这为什么不好?

🌐 Why is this bad?

JSDoc '@deprecated' 标签可用于记录某段代码 被贬低。最好避免使用标记为弃用的代码。 本规则报告任何标记为“@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 标签,允许编辑器以可视化方式标记已弃用的代码——通常是使用删除线。然而,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.

例子

🌐 Examples

此规则的 错误 代码示例:

🌐 Examples of incorrect code for this rule:

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

await apiV1(); // Using deprecated function

import { parse } from "node:url";
// 'parse' is deprecated. Use the WHATWG URL API instead.
const url = parse("/foo");

此规则的正确代码示例:

🌐 Examples of correct code for this rule:

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

await apiV2(); // Using non-deprecated function

// Modern Node.js API, uses `new URL()`
const url2 = new URL("/foo", "http://www.example.com");

配置

🌐 Configuration

此规则接受一个具有以下属性的配置对象:

🌐 This rule accepts a configuration object with the following properties:

允许

🌐 allow

类型:array

🌐 type: array

默认:[]

🌐 default: []

允许使用的类型或值说明符数组,即使它们已被弃用。使用此方法可以允许你有意继续使用的特定已弃用 API。

🌐 An array of type or value specifiers that are allowed to be used even if deprecated. Use this to allow specific deprecated APIs that you intentionally want to continue using.

allow[n]

类型:string

🌐 type: string

用于匹配特定声明的类型或值说明符

🌐 Type or value specifier for matching specific declarations

支持四种类型的说明符:

🌐 Supports four types of specifiers:

  1. 字符串说明符(已弃用):按名称通用匹配
json
"Promise"
  1. 文件指定符:匹配本地文件中声明的类型/值
json
{ "from": "file", "name": "MyType" }
{ "from": "file", "name": ["Type1", "Type2"] }
{ "from": "file", "name": "MyType", "path": "./types.ts" }
  1. Lib 指定符:匹配 TypeScript 内置的库类型
json
{ "from": "lib", "name": "Promise" }
{ "from": "lib", "name": ["Promise", "PromiseLike"] }
  1. 包指定器:匹配来自 npm 包的类型/值
json
{ "from": "package", "name": "Observable", "package": "rxjs" }
{ "from": "package", "name": ["Observable", "Subject"], "package": "rxjs" }

如何使用

🌐 How to use

要通过配置文件或命令行启用此规则,你可以使用:

🌐 To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "typescript/no-deprecated": "error"
  }
}
bash
oxlint --type-aware --deny typescript/no-deprecated

参考文献

🌐 References