Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions crates/oxc_linter/src/rules/typescript/prefer_as_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,36 @@ pub struct PreferAsConst;

declare_oxc_lint!(
/// ### What it does
/// Enforce the use of as const over literal type.
///
/// Enforce the use of `as const` over literal type.
///
/// ### Why is this bad?
/// There are two common ways to tell TypeScript that a literal value should be interpreted as its literal type (e.g. 2) rather than general primitive type (e.g. number);
///
/// as const: telling TypeScript to infer the literal type automatically
/// as with the literal type: explicitly telling the literal type to TypeScript
/// There are two common ways to tell TypeScript that a literal value should be interpreted as
/// its literal type (e.g. `2`) rather than general primitive type (e.g. `number`);
///
/// `as const`: telling TypeScript to infer the literal type automatically
/// `as` with the literal type: explicitly telling the literal type to TypeScript
///
/// `as const` is generally preferred, as it doesn't require re-typing the literal value.
/// This rule reports when an `as` with an explicit literal type can be replaced with an `as const`.
///
/// as const is generally preferred, as it doesn't require re-typing the literal value.
/// This rule reports when an as with an explicit literal type can be replaced with an as const.
/// ### Examples
///
/// ### Example
/// Examples of **incorrect** code for this rule:
/// ```ts
/// let bar: 2 = 2;
/// let foo = { bar: 'baz' as 'baz' };
/// ```
///
/// Examples of **correct** code for this rule:
/// ```ts
/// let foo = 'bar';
/// let foo = 'bar' as const;
/// let foo: 'bar' = 'bar' as const;
/// let bar = 'bar' as string;
/// let foo = { bar: 'baz' };
/// ```
PreferAsConst,
typescript,
correctness,
Expand Down