Skip to content
← Back to rules

typescript/no-deprecated Pedantic

💭 This rule requires type information.

功能说明

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

为什么这样做是不好的?

JSDoc 的 @deprecated 标签可用于文档化某些代码已被弃用。最好避免使用标记为弃用的代码。此规则会报告任何对标记为 @deprecated 的代码的引用。

TypeScript 可以识别 @deprecated 标签,允许编辑器在视觉上指示已弃用的代码——通常使用删除线。但是,TypeScript 本身不会为已弃用的代码报告类型错误。

示例

此规则的错误代码示例:

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");

此规则的正确代码示例:

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");

配置

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

allow

类型:array

默认值:[]

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

allow[n]

类型:object | string

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

支持四种类型的说明符:

  1. 字符串说明符(已弃用):按名称进行通用匹配
json
"Promise"
  1. 文件说明符:匹配本地文件中声明的类型/值
json
{ "from": "file", "name": "MyType" }
{ "from": "file", "name": ["Type1", "Type2"] }
{ "from": "file", "name": "MyType", "path": "./types.ts" }
  1. 库说明符:匹配 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" }
allow[n].from

类型:"file"

allow[n].name

类型:array | string

名称说明符,可以是单个字符串或字符串数组

allow[n].name[n]

类型:string

allow[n].path

类型:string

可选的文件路径,用于指定类型或值必须声明的位置。如果省略,将匹配所有文件。

如何使用

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

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/no-deprecated": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/no-deprecated": "error",
  },
});
bash
oxlint --type-aware --deny typescript/no-deprecated

版本

此规则自 v1.26.0 起添加。

参考