Skip to content

Commit 187fe39

Browse files
authoredMar 17, 2025··
docs(linter): Add correctness examples to typescript-prefer-as-const (#9805)
Adds correctness examples to docs in line with #6050
1 parent 4e39ba0 commit 187fe39

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed
 

‎crates/oxc_linter/src/rules/typescript/prefer_as_const.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,36 @@ pub struct PreferAsConst;
2323

2424
declare_oxc_lint!(
2525
/// ### What it does
26-
/// Enforce the use of as const over literal type.
26+
///
27+
/// Enforce the use of `as const` over literal type.
28+
///
2729
/// ### Why is this bad?
28-
/// 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);
2930
///
30-
/// as const: telling TypeScript to infer the literal type automatically
31-
/// as with the literal type: explicitly telling the literal type to TypeScript
31+
/// There are two common ways to tell TypeScript that a literal value should be interpreted as
32+
/// its literal type (e.g. `2`) rather than general primitive type (e.g. `number`);
33+
///
34+
/// `as const`: telling TypeScript to infer the literal type automatically
35+
/// `as` with the literal type: explicitly telling the literal type to TypeScript
36+
///
37+
/// `as const` is generally preferred, as it doesn't require re-typing the literal value.
38+
/// This rule reports when an `as` with an explicit literal type can be replaced with an `as const`.
3239
///
33-
/// as const is generally preferred, as it doesn't require re-typing the literal value.
34-
/// This rule reports when an as with an explicit literal type can be replaced with an as const.
40+
/// ### Examples
3541
///
36-
/// ### Example
42+
/// Examples of **incorrect** code for this rule:
3743
/// ```ts
3844
/// let bar: 2 = 2;
3945
/// let foo = { bar: 'baz' as 'baz' };
4046
/// ```
47+
///
48+
/// Examples of **correct** code for this rule:
49+
/// ```ts
50+
/// let foo = 'bar';
51+
/// let foo = 'bar' as const;
52+
/// let foo: 'bar' = 'bar' as const;
53+
/// let bar = 'bar' as string;
54+
/// let foo = { bar: 'baz' };
55+
/// ```
4156
PreferAsConst,
4257
typescript,
4358
correctness,

0 commit comments

Comments
 (0)
Please sign in to comment.