Skip to content

Latest commit

 

History

History
40 lines (27 loc) · 676 Bytes

prefer-as-const.md

File metadata and controls

40 lines (27 loc) · 676 Bytes

Prefer usage of as const over literal type (prefer-as-const)

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

Rule Details

Examples of code for this rule:

❌ Incorrect

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

✅ Correct

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' };

When Not To Use It

If you are using TypeScript < 3.4

Attributes

  • ✅ Recommended
  • 🔧 Fixable
  • 💭 Requires type information