src/content/docs/linter/rules/use-as-const-assertion.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="TypeScript and TSX" icon="seti:typescript"> ## Summary - Rule available since: `v1.3.0` - Diagnostic Category: [`lint/style/useAsConstAssertion`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`@typescript-eslint/prefer-as-const`](https://typescript-eslint.io/rules/prefer-as-const){
"linter": {
"rules": {
"style": {
"useAsConstAssertion": "error"
}
}
}
}
Enforce the use of as const over literal type and type annotation.
In TypeScript, there are three common ways to specify that a value is of a specific type such as 2 and not a general type such as number:
as const: telling TypeScript to infer the literal type automaticallyas <literal>: explicitly telling the literal type to TypeScriptThe rule suggests to use as const when you're using as with a literal type or type annotation, since as const is simpler and doesn't require retyping the value.
let bar: 2 = 2;
let foo = { bar: 'baz' as 'baz' };
let foo = 'bar';
let foo = 'bar' as const;
let foo: 'bar' = 'bar' as const;
let bar = 'bar' as string;
let foo = { bar: 'baz' };