Skip to content
← Back to rules

typescript/prefer-as-const 正确性

This rule is turned on by default.
🛠️ An auto-fix is available for this rule for some violations.

作用

强制优先使用 as const,而不是字面量类型。

为什么这不好?

有两种常见方式可以告诉 TypeScript,一个字面量值应当被解释为 其字面量类型(例如 2),而不是通用的基本类型(例如 number);

as const:告诉 TypeScript 自动推断字面量类型 as 加字面量类型:显式告诉 TypeScript 该字面量类型

通常更推荐使用 as const,因为它不需要重新输入字面量值。 当显式字面量类型的 as 可以被 as const 替换时,此规则会报告。

示例

此规则的错误代码示例:

ts
let bar: 2 = 2;
let foo = { bar: "baz" as "baz" };

此规则的正确代码示例:

ts
let foo = "bar";
let foo = "bar" as const;
let foo: "bar" = "bar" as const;
let bar = "bar" as string;
let foo = { bar: "baz" };

如何使用

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

json
{
  "rules": {
    "typescript/prefer-as-const": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "typescript/prefer-as-const": "error",
  },
});
bash
oxlint --deny typescript/prefer-as-const

版本

此规则是在 v0.0.8 中添加的。

参考