{"record":{"id":"320a238f17b224db","repo":"oxc-project/oxc","slug":"do-not-use-any-type-assertions","errorCode":null,"errorMessage":"Do not use any type assertions.","messagePattern":"Do not use any type assertions\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/typescript/consistent_type_assertions.rs","lineNumber":32,"sourceCode":"    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[] = [ ... ]`.\")\n        .with_help(\"Replace the array literal type assertion with a type annotation. For example, change `const x = [1, 2] as Type[]` to `const x: Type[] = [1, 2]`. Alternatively, use `const x = [1, 2] satisfies Type[]` if you want TypeScript to infer the exact array type.\")\n        .with_note(\"Type assertions on array literals can hide errors where the array doesn't actually match the asserted type. Using type annotations or `satisfies` ensures TypeScript verifies that the array matches the expected type.\")\n        .with_label(span)\n}","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/typescript/consistent_type_assertions.rs#L14-L50","documentation":"Warning from typescript/consistent-type-assertions via never_diagnostic() (crates/oxc_linter/src/rules/typescript/consistent_type_assertions.rs:32). With assertionStyle 'never', all type assertions are banned: the help suggests a type annotation ('const x: T = value') or the 'satisfies' operator, and the note explains assertions bypass type checking and can hide errors.","triggerScenarios":"oxlint configured with 'consistent-type-assertions': ['error', { assertionStyle: 'never' }] and any 'value as T' or '<T>value' appears in the file — the visitor flags every TSTypeAssertion and TSAsExpression node.","commonSituations":"Safety-critical or strict-mode codebases that forbid unchecked casts; adopting the strict preset after a codebase relied heavily on 'as any'-style escapes; assertions around JSON.parse, event targets, and third-party APIs being the usual offenders.","solutions":["Replace the assertion with a type annotation: 'const x: T = value'","Use 'value satisfies T' when you want to keep the inferred type while checking compatibility","Add a runtime guard (typeof/instancezof checks or a validator) when the source value is untrusted, then narrow instead of asserting","Where a cast is unavoidable (e.g. proven invariants), add an explained eslint-disable-next-line comment"],"exampleFix":"// before\nconst x = JSON.parse(raw) as Config;\n\n// after\nconst x: Config = JSON.parse(raw); // or parse with zod/typebox and narrow","handlingStrategy":"type-guard","validationCode":"const ANY_CAST = /\\bas\\s+(?!const\\b)\\w+|<\\s*\\w+\\s*>\\s*\\w\\s*[;=(]/;\nfor (const line of source.split('\\n')) {\n  if (ANY_CAST.test(line)) fail('type assertion used; assertionStyle is \"never\"', line);\n}","typeGuard":"function assertIs<T>(v: unknown, check: (x: unknown) => x is T): T {\n  if (!check(v)) throw new TypeError('value is not of expected type');\n  return v;\n}","tryCatchPattern":null,"preventionTips":["Validate untrusted data at the boundary (zod/typebox/custom guards) instead of casting","Annotate declarations rather than asserting expressions","Reserve inline disables for proven invariants, each with a reason"],"tags":["typescript","oxlint","lint","type-assertion","type-safety","strict-mode"],"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"}