{"record":{"id":"7c63b990fb341112","repo":"oxc-project/oxc","slug":"use-as-cast-instead-of-cast","errorCode":null,"errorMessage":"Use `as {cast}` instead of `<{cast}>`.","messagePattern":"Use `as (.+?)` instead of `<(.+?)>`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/typescript/consistent_type_assertions.rs","lineNumber":28,"sourceCode":"use schemars::JsonSchema;\nuse serde::Deserialize;\n\nuse crate::{\n    AstNode,\n    ast_util::outermost_paren_parent,\n    context::{ContextHost, LintContext},\n    fixer::{RuleFix, RuleFixer},\n    rule::{DefaultRuleConfig, Rule},\n};\n\nfn use_angle_bracket_diagnostic(cast: &str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Use `<{cast}>` instead of `as {cast}`.\"))\n        .with_help(format!(\"Replace `as {cast}` with `<{cast}>`. For example, change `value as {cast}` to `<{cast}>value`.\"))\n        .with_label(span)\n}\n\nfn use_as_diagnostic(cast: &str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Use `as {cast}` instead of `<{cast}>`.\")).with_label(span)\n}\n\nfn never_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not use any type assertions.\")\n        .with_help(\"Remove the type assertion and use a type annotation instead. For example, change `const x = value as Type` to `const x: Type = value`. Alternatively, use the `satisfies` operator: `const x = value satisfies Type`.\")\n        .with_note(\"Type assertions bypass TypeScript's type checking and can hide type errors. Using type annotations or the `satisfies` operator provides better type safety while still allowing TypeScript to infer types where appropriate.\")\n        .with_label(span)\n}\n\nfn unexpected_object_type_assertion_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Always prefer `const x: T = { ... }`.\")\n        .with_help(\"Replace the object literal type assertion with a type annotation. For example, change `const x = { a: 1 } as Type` to `const x: Type = { a: 1 }`. Alternatively, use `const x = { a: 1 } satisfies Type` if you want TypeScript to infer the exact shape.\")\n        .with_note(\"Type assertions on object literals can hide errors where the object doesn't actually match the asserted type. Using type annotations or `satisfies` ensures TypeScript verifies that the object matches the expected type.\")\n        .with_label(span)\n}\n\nfn unexpected_array_type_assertion_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Always prefer `const x: T[] = [ ... ]`.\")","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/typescript/consistent_type_assertions.rs#L10-L46","documentation":"Warning from typescript/consistent-type-assertions via use_as_diagnostic() (crates/oxc_linter/src/rules/typescript/consistent_type_assertions.rs:28). With the default assertionStyle 'as', type assertions must use 'value as T'; it fires on legacy angle-bracket syntax '<T>value' and shows both forms in the message.","triggerScenarios":"oxlint runs with the default (or explicit) assertionStyle 'as' and the file contains 'const n = <string>value;' or '<HTMLElement>document.getElementById(x)' — any TSTypeAssertion node hit by the visitor.","commonSituations":"Modernizing pre-2019 TypeScript that used angle-bracket casts; copying old Stack Overflow answers; React codebases where stray angle-bracket assertions collide with JSX parsing rules (they are illegal in .tsx anyway).","solutions":["Rewrite '<T>value' as 'value as T'","Run 'oxlint --fix' for a mechanical repo-wide conversion","Prefer type annotations or 'satisfies' where the assertion is not truly needed (see the never_diagnostic guidance in the same rule)"],"exampleFix":"// before\nconst n = <string>value;\n\n// after\nconst n = value as string;","handlingStrategy":"validation","validationCode":"const ANGLE = /=\\s*<\\s*\\w+\\s*>\\s*\\w/;\nfor (const line of source.split('\\n')) {\n  if (ANGLE.test(line)) fail('angle-bracket assertion used; use \"as\"', line);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Modernize copied legacy snippets before committing","Run oxlint --fix over migrated codebases once","Prefer 'satisfies' or annotations when the cast is not load-bearing"],"tags":["typescript","oxlint","lint","type-assertion","as-cast","style"],"backgroundTag":"type-assertion-style","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"}