Skip to content

asConstAssertions

Reports using explicit literal types when as const can be used.

✅ This rule is included in the ts stylistic preset.

There are two common ways to tell TypeScript that a literal value should be interpreted as its literal type rather than a general primitive type:

  • as const: telling TypeScript to infer the literal type automatically
  • as with an explicit literal type: explicitly telling TypeScript the literal type

as const is generally preferred as it doesn’t require repeating the literal value. This rule reports when an explicit literal type can be replaced with as const.

let value: 2 = 2;
let value = "hello" as "hello";
const flag: true = true;

This rule is not configurable.

If your codebase consistently uses explicit literal types for documentation purposes, or if you don’t have a preference between the two styles, you may disable this rule.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.