oxc-project/oxc · warning · OxcDiagnostic
Use `as {cast}` instead of `<{cast}>`.
Error message
Use `as {cast}` instead of `<{cast}>`. What it means
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.
Source
Thrown at crates/oxc_linter/src/rules/typescript/consistent_type_assertions.rs:28
use schemars::JsonSchema;
use serde::Deserialize;
use crate::{
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[] = [ ... ]`.")View on GitHub (pinned to e1e7af627c)
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)
Example fix
// before const n = <string>value; // after const n = value as string;
Defensive patterns
Strategy: validation
Validate before calling
const ANGLE = /=\s*<\s*\w+\s*>\s*\w/;
for (const line of source.split('\n')) {
if (ANGLE.test(line)) fail('angle-bracket assertion used; use "as"', line);
} Prevention
- Modernize copied legacy snippets before committing
- Run oxlint --fix over migrated codebases once
- Prefer 'satisfies' or annotations when the cast is not load-bearing
When it happens
Trigger: 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.
Common situations: 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).
Related errors
- Use `<{cast}>` instead of `as {cast}`.
- Type can be trivially inferred from the initializer
- Prefer using inline type specifiers instead of a top-level t
- Prefer using a top-level type-only import instead of inline
- Prefer using a top-level type-only import instead of inline
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/7c63b990fb341112.
Report an issue: GitHub.