Urara-Blog/node_modules/.pnpm-store/v3/files/2c/0830b0013a6b5a9eb9a2a9467cd97e3b943db7ceb91d4170b9dab55f816b726b3461d9eaa9fcaedd91d07602ecd5b4bd561582066a878e17988f6956c2c748
2022-08-14 01:14:53 +08:00

53 lines
895 B
Text

---
description: 'Enforce the use of `as const` over literal type.'
---
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/prefer-as-const** for documentation.
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
```ts
let bar: 2 = 2;
let foo = <'bar'>'bar';
let foo = { bar: 'baz' as 'baz' };
```
### ✅ Correct
```ts
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
```jsonc
// .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