oxc-project/oxc · warning · OxcDiagnostic
Do not use any type assertions.
Error message
Do not use any type assertions.
What it means
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.
Source
Thrown at crates/oxc_linter/src/rules/typescript/consistent_type_assertions.rs:32
AstNode,
ast_util::outermost_paren_parent,
context::{ContextHost, LintContext},
fixer::{RuleFix, RuleFixer},
rule::{DefaultRuleConfig, Rule},
};
fn use_angle_bracket_diagnostic(cast: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Use `<{cast}>` instead of `as {cast}`."))
.with_help(format!("Replace `as {cast}` with `<{cast}>`. For example, change `value as {cast}` to `<{cast}>value`."))
.with_label(span)
}
fn use_as_diagnostic(cast: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Use `as {cast}` instead of `<{cast}>`.")).with_label(span)
}
fn never_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not use any type assertions.")
.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`.")
.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.")
.with_label(span)
}
fn unexpected_object_type_assertion_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Always prefer `const x: T = { ... }`.")
.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.")
.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.")
.with_label(span)
}
fn unexpected_array_type_assertion_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Always prefer `const x: T[] = [ ... ]`.")
.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.")
.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.")
.with_label(span)
}View on GitHub (pinned to e1e7af627c)
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
Example fix
// before const x = JSON.parse(raw) as Config; // after const x: Config = JSON.parse(raw); // or parse with zod/typebox and narrow
Defensive patterns
Strategy: type-guard
Validate before calling
const ANY_CAST = /\bas\s+(?!const\b)\w+|<\s*\w+\s*>\s*\w\s*[;=(]/;
for (const line of source.split('\n')) {
if (ANY_CAST.test(line)) fail('type assertion used; assertionStyle is "never"', line);
} Type guard
function assertIs<T>(v: unknown, check: (x: unknown) => x is T): T {
if (!check(v)) throw new TypeError('value is not of expected type');
return v;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Always prefer `const x: T = { ... }`.
- encountered allocation error
- Prefer explicitly define the object shape
- Don't use `Function` as a type
- 'The `Object` type actually means "any non-nullish value"
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/320a238f17b224db.
Report an issue: GitHub.