close

prefer-as-const

Configuration

PresetConfigured Value
✅ ts.configs.recommended"error"
rslint.config.ts
import { defineConfig, ts } from '@rslint/core';

export default defineConfig([
  ts.configs.recommended,
  {
    rules: {
      '@typescript-eslint/prefer-as-const': 'error',
    },
  },
]);

Rule Details

Enforce the use of as const over literal type assertions. Using as const is preferred because it more clearly expresses intent, consistently applies to all literal values, and enables deeper immutability for objects and arrays.

This rule flags type assertions (as or angle-bracket style) and type annotations on variable or property declarations where the asserted/annotated type is a literal type that matches the literal value.

Examples of incorrect code for this rule:

let foo = 'bar' as 'bar';
let baz = 1 as 1;
let qux = <'bar'>'bar';
let x: 10 = 10;

Examples of correct code for this rule:

let foo = 'bar' as const;
let baz = 1 as const;
let arr = [1, 2, 3] as const;

Original Documentation