Skip to content
← Back to rules

typescript/no-restricted-types Restriction

🛠️ 💡 An auto-fix and a suggestion are available for this rule.

它的作用

禁止使用某些类型。

这为什么不好?

一些内置类型有别名,而另一些类型被认为是危险或有害的。 通常,禁止某些类型有助于保持一致性和安全性。

示例

给定 { "types": { "Foo": { "message": "请改用 Bar", "fixWith": "Bar" } } }

此规则的错误代码示例:

ts
let value: Foo;

此规则的正确代码示例:

ts
let value: Bar;

此规则的其他配置选项示例:

  • 仅通过消息禁止 Foo 类型,不提供修复或建议: { "types": { "Foo": "请改用 OtherType。" } }

  • 通过建议禁止 Bar 类型: { "types": { "Bar": { "message": "请避免使用 Bar。", "suggest": "BazQux" } } }

  • 使用通用消息禁止 Object 类型: { "types": { "Object": true } }

配置

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

types

type: object

default: {}

类型名称到禁止配置的映射。

如何使用

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

json
{
  "rules": {
    "typescript/no-restricted-types": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "typescript/no-restricted-types": "error",
  },
});
bash
oxlint --deny typescript/no-restricted-types

版本

此规则在 v1.31.0 中添加。

参考资料