prefer-as-const
What it does
Section titled “What it does”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.
Why it matters
Section titled “Why it matters”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.
Example
Section titled “Example”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"; }Configuration
Section titled “Configuration”js: lint: rules: prefer-as-const: warn # off (default), warn, errorEnabled 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.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| typescript-eslint | prefer-as-const |
full |