Skip to content

prefer-as-const

Reports a literal type that only repeats the literal it is given: let a: 'a' = 'a', 'a' as 'a' and <'a'>'a'. A type that differs from the value (let a: 'a' = 'b' is a type error, not reported) and as const are not reported.

The literal is written twice and the two copies can drift apart when one is edited. as const says it once and keeps the literal type.

To fix, write let a = 'a' as const.

Reported
let a: "a" = "a", b: "a" = 'a', c: 1 = 1, d: string = "a";
class C { p: "a" = "a"; q = 1 as 1; r = <"a">"a"; s = "a" as "b"; }
js:
lint:
rules:
prefer-as-const: warn # off (default), warn, error

Enabled by the presets: js/typescript-eslint-recommended, js/oxlint.

Suppress one line with sudsy-disable-next-line prefer-as-const in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
typescript-eslint prefer-as-const full