Skip to main content

prefer-as-const

Enforce the use of as const over literal type.

This rule recommends usage of const assertion when type primitive value is equal to type.

Attributes

  • Included in configs
    • ✅ Recommended
    • 🔒 Strict
  • Fixable
    • 🔧 Automated Fixer
    • 🛠 Suggestion Fixer
  • 💭 Requires type information

Rule Details

Examples of code for this rule:

let bar: 2 = 2;
let foo = <'bar'>'bar';
let foo = { bar: 'baz' as 'baz' };

Options

// .eslintrc.json
{
"rules": {
"@typescript-eslint/prefer-as-const": "error"
}
}

This rule is not configurable.

When Not To Use It

If you are using TypeScript < 3.4