oxc-project/oxc · warning

Type can be trivially inferred from the initializer

Error message

Type can be trivially inferred from the initializer

What it means

Diagnostic from oxlint's port of @typescript-eslint/no-inferrable-types. It reports type annotations that TypeScript infers trivially from the initializer — `const n: number = 5`, `let flag: boolean = true`, regex, string literal initializers, defaulted parameters `function f(a: number = 1)`, class properties. The annotation is redundant noise that can even mask a type change (initializer changes type while annotation keeps the declared type). Options `ignoreParameters` and `ignoreProperties` exempt params and class fields.

Source

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

    ast::{
        ChainElement, Expression, FormalParameter, TSLiteral, TSType, TSTypeAnnotation, TSTypeName,
        UnaryOperator,
    },
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use schemars::JsonSchema;
use serde::Deserialize;

use crate::{
    AstNode,
    context::LintContext,
    rule::{DefaultRuleConfig, Rule},
};

fn no_inferrable_types_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Type can be trivially inferred from the initializer")
        .with_help("Remove the type annotation")
        .with_label(span)
}

#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct NoInferrableTypes {
    /// When set to `true`, ignores type annotations on function parameters.
    ignore_parameters: bool,
    /// When set to `true`, ignores type annotations on class properties.
    ignore_properties: bool,
}

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow explicit type declarations for variables or parameters initialized to a number, string, or boolean.
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Delete the annotation and let inference work: `const n = 5`.
  2. For defaulted params you want to keep strict, either annotate a wider type (`function f(a: number | undefined = 1)`) or enable ignoreParameters.
  3. Set `ignoreProperties`/`ignoreParameters` if class fields/params are your accepted style.
  4. Disable the rule repo-wide if your team mandates explicit types.

Example fix

// before
const port: number = 8080;
let verbose: boolean = false;
class Config { timeout: number = 30; }

// after
const port = 8080;
let verbose = false;
class Config { timeout = 30; }
Defensive patterns

Strategy: validation

Validate before calling

oxlint --ts-plugin src/ # no-inferrable-types; add ignoreParameters/ignoreProperties if needed

Type guard

// when you DO need a wider declared type than the initializer, say it explicitly:
let status: 'idle' | 'busy' = 'idle'; // union is not trivially inferrable → rule stays quiet

Prevention

When it happens

Trigger: Variable/property declarations and defaulted parameters whose annotation is one of the trivially-inferable kinds while an initializer literal is present; per the config, parameter annotations are exempt when ignoreParameters=true and class-property annotations when ignoreProperties=true.

Common situations: Developers from Java/C# backgrounds annotating everything; IDE auto-import/quick-fix inserting annotations; explicit-type style guides; defaulted function parameters annotated `a: number = 0`.

Related errors


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