简中文档

prefer-as-const

NOTE: this rule is part of the 推荐 rule set.
Enable full set in deno.json:
{
  "lint": {
    "rules": {
      "tags": ["推荐"]
    }
  }
}
Enable full set using the Deno CLI:
deno lint --rules-tags=推荐
This rule can be explictly included to or excluded from the rules present in the current tag by adding it to the include or exclude array in deno.json:
{
  "lint": {
    "rules": {
      "include": ["prefer-as-const"],
      "exclude": ["prefer-as-const"]
    }
  }
}

推荐使用常量断言(as const)而不是显式指定字面量类型或使用类型断言。

在声明一个新的原始字面量类型变量时,有三种方式:

  1. 添加显式类型注解
  2. 使用普通类型断言(如 as "foo"<"foo">
  3. 使用常量断言(as const

此 lint 规则建议使用常量断言,因为这通常会导致更安全的代码。有关常量断言的更多细节,请参见 官方手册

无效:

let a: 2 = 2; // 类型注解
let b = 2 as 2; // 类型断言
let c = <2> 2; // 类型断言
let d = { foo: 1 as 1 }; // 类型断言

有效:

let a = 2 as const;
let b = 2 as const;
let c = 2 as const;
let d = { foo: 1 as const };

let x = 2;
let y: string = "hello";
let z: number = someVariable;

你找到了你需要的东西吗?

编辑此页面
隐私政策