JS.TS.PREFER.AS.CONST

Prefer usage of 'as const' over literal type

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

Rule Details

Examples of code for this rule:

<!--tabs-->

Incorrect

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

Correct

Copy
let foo = 'bar';
let foo = 'bar' as const;
let foo: 'bar' = 'bar' as const;
let bar = 'bar' as string;
let foo = <string>'bar';
let foo = { bar: 'baz' };

<!--/tabs-->

Options

Copy
// .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

The content on this page is adapted from the ESLint User Guide. Copyright © OpenJS Foundation and other contributors, www.openjsf.org. All rights reserved. https://eslint.org/docs/rules/