目录

@typescript-eslint/prefer-as-const

Enforce the use of as const over literal type.

There are two ways to indicate that a literal value should be interpreted as its literal type: as const and as with the literal type. The as const approach is recommended.

This rule applies only to .js and .ts files.

Rule Configuration

// code-linter.json5
{
  "rules": {
    "@typescript-eslint/prefer-as-const": "error"
  }
}

Options

This rule is not configurable.

Example of Correct Code

export const foo1 = 'bar';
export const foo2 = 'bar' as const;
export const foo3: 'bar' = 'bar' as const;
export const bar4 = 'bar' as string;
export const foo6 = { bar: 'baz' };

Example of Incorrect Code

export const bar: 1 = 1;
export const foo1 = <'bar'>'bar';
export const foo2 = { bar: 'baz' as 'baz' };

Rule Set

plugin:@typescript-eslint/all

For details about how to configure Code Linter check rules, see Code Linter.