oxc-project/oxc · warning · OxcDiagnostic

Use `<{cast}>` instead of `as {cast}`.

Error message

Use `<{cast}>` instead of `as {cast}`.

What it means

Warning from typescript/consistent-type-assertions via use_angle_bracket_diagnostic() (crates/oxc_linter/src/rules/typescript/consistent_type_assertions.rs:22). With assertionStyle 'angle-bracket', type assertions must use the legacy '<T>value' syntax; it fires on 'value as T' and shows the cast string in both message and help.

Source

Thrown at crates/oxc_linter/src/rules/typescript/consistent_type_assertions.rs:22

    AstKind,
    ast::{Expression, TSType, TSTypeName},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
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.")

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Rewrite 'value as T' as '<T>value' per the rule's fixer and help example
  2. Run 'oxlint --fix' so the rule rewrites all 'as' casts mechanically
  3. If the codebase is .tsx or modern, prefer the default 'as' style and remove the custom assertionStyle

Example fix

// before
const n = value as string;

// after
const n = <string>value;
Defensive patterns

Strategy: validation

Validate before calling

const AS_CAST = /\bas\s+\w+/;
for (const line of source.split('\n')) {
  if (AS_CAST.test(line)) fail("'as' cast used; angle-bracket style required", line);
}

Prevention

When it happens

Trigger: oxlint configured with 'consistent-type-assertions': ['error', { assertionStyle: 'angle-bracket' }] and the file contains 'const n = value as string;' or 'foo as unknown as Bar'; the span labels the assertion expression.

Common situations: Older TypeScript codebases that standardized on angle-bracket casts before 'as' became dominant; enabling the non-default option on code written with modern style; note angle-bracket assertions are invalid in .tsx files, so the option is rare in React projects.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/9017f13166b21f57. Report an issue: GitHub.