{"record":{"id":"789ad6982d5524cd","repo":"oxc-project/oxc","slug":"expected-a-const-assertion-instead-of-a-literal","errorCode":null,"errorMessage":"Expected a `const` assertion instead of a literal type annotation.","messagePattern":"Expected a `const` assertion instead of a literal type annotation\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/typescript/prefer_as_const.rs","lineNumber":16,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{BindingPattern, Expression, TSLiteral, TSType, TSTypeAnnotation},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{\n    AstNode,\n    context::{ContextHost, LintContext},\n    rule::Rule,\n};\n\nfn prefer_as_const_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Expected a `const` assertion instead of a literal type annotation.\")\n        .with_help(\"You should use `as const` instead of type annotation.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferAsConst;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Enforce the use of `as const` over literal types.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// There are two common ways to tell TypeScript that a literal value should be interpreted as\n    /// its literal type (e.g. `2`) rather than general primitive type (e.g. `number`);\n    ///\n    /// `as const`: telling TypeScript to infer the literal type automatically","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/typescript/prefer_as_const.rs#L1-L34","documentation":"Raised by typescript/prefer_as_const when a variable or property is annotated with a literal type (e.g. `let x: 2 = 2;` or `x: 'a' = 'a'`) instead of using a `const` assertion (`x = 2 as const`). At the throw site the annotation pins the type to the literal while duplicating the value, and `as const` expresses the same intent more idiomatically while also working where annotations don't (deep object/array literals). prefer_as_const_diagnostic fires from check_and_report_as_expression and check_and_report_type_annotation for literal-type annotations whose initializer matches.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/typescript/prefer_as_const.rs:16 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use `as const`: let x = \"foo\" as const.","Remove the redundant literal type annotation where possible."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}