asConstAssertions
Reports using explicit literal types when
as constcan 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 automaticallyaswith 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.
Examples
Section titled “Examples”let value: 2 = 2;let value = "hello" as "hello";const flag: true = true;let value = 2 as const;let value = "hello" as const;let value = "hello" as string;const flag = true as const;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useAsConstAssertion - Deno:
prefer-as-const - ESLint:
@typescript-eslint/prefer-as-const - Oxlint:
typescript/prefer-as-const
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.