# @typescript-eslint/prefer-as-const

> Require `as const` instead of repeating a literal as its own type

`"dark" as "dark"` and `let theme: "dark" = "dark"` write the same literal twice, and you need to change both every time. `as const` gives you the same narrow type and takes it from the value.

The rule checks every `as` assertion, and the type annotations on variables and class fields. It only reports a type that's the same literal as the value.

`--fix` rewrites `"dark" as "dark"` to `"dark" as const`. A type annotation is reported but not fixed.

This rule is on by default in TypeScript files.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

On by default as a warning. To turn it off:

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/prefer-as-const" = "off" # or "error"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 3} */}
```ts
let theme = "dark" as "dark";
let retries: 3 = 3;

class Request {
  method: "GET" = "GET";
}
```

Not reported:

{/* good */}
```ts
let theme = "dark" as const;
let retries = 3 as const;

class Request {
  method = "GET" as const;
}

let mode: "dark" | "light" = "dark";
let size = "small" as "small" | "large";
```
