oxc-project/oxc · warning · OxcDiagnostic

The generic type arguments should be specified as part of th

Error message

The generic type arguments should be specified as part of the type annotation.

What it means

Warning from typescript/consistent-generic-constructors via ..._prefer_annotation() (crates/oxc_linter/src/rules/typescript/consistent_generic_constructors.rs:24). With the default option 'prefer-type-annotation', generic type arguments belong on the variable's type annotation, not on the constructor call; it fires when a constructor call like 'new Map<string, number>()' supplies them instead.

Source

Thrown at crates/oxc_linter/src/rules/typescript/consistent_generic_constructors.rs:24

        TSTypeParameterInstantiation, TSTypeReference,
    },
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::IsGlobalReference;
use oxc_span::{GetSpan, Span};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

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

fn consistent_generic_constructors_diagnostic_prefer_annotation(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(
        "The generic type arguments should be specified as part of the type annotation.",
    )
    .with_help("Move the generic type to the type annotation")
    .with_label(span)
}

fn consistent_generic_constructors_diagnostic_prefer_constructor(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(
        "The generic type arguments should be specified as part of the constructor type arguments.",
    )
    .with_help("Move the type annotation to the constructor")
    .with_label(span)
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, JsonSchema)]
pub struct ConsistentGenericConstructors(PreferGenericType);

#[derive(Debug, Default, Clone, Serialize, Deserialize, JsonSchema)]

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Move the type arguments to the annotation: 'const m: Map<string, number> = new Map();'
  2. Normalize the codebase with a codemod (ast-grep or a lint --fix pass, since this rule provides a fixer via RuleFixer)
  3. If your team prefers constructor args, set 'prefer-constructor-type-arguments' in .oxlintrc.json

Example fix

// before
const roles = new Map<string, Role>();

// after
const roles: Map<string, Role> = new Map();
Defensive patterns

Strategy: validation

Validate before calling

const CTOR_GENERICS = /=\s*new\s+\w+\s*<[^>]+>\s*\(/;
for (const line of source.split('\n')) {
  if (CTOR_GENERICS.test(line)) fail('generics on constructor; move to type annotation', line);
}

Prevention

When it happens

Trigger: oxlint runs with the default (or explicit) 'constructor-type' = 'prefer-type-annotation' and sees 'const m = new Map<string, number>();', 'const a = new Array<string>()', or 'new Set<Foo>()' where the type arguments sit on the constructor expression.

Common situations: Enabling recommended configs on a codebase that habitually writes 'new Map<K,V>()' inline; code copied from documentation examples that parameterize the constructor; mixed styles after merging teams.

Related errors


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