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 constructor type arguments.

What it means

Warning from typescript/consistent-generic-constructors via ..._prefer_constructor() (crates/oxc_linter/src/rules/typescript/consistent_generic_constructors.rs:32). With the option 'prefer-constructor-type-arguments', generic type arguments belong on the constructor call; it fires when they appear on the variable's type annotation while the constructor call omits them.

Source

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

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)]
#[serde(rename_all = "kebab-case")]
enum PreferGenericType {
    /// Type arguments that only appear on the type annotation are disallowed.
    #[default]
    Constructor,
    /// Type arguments that only appear on the constructor are disallowed.
    TypeAnnotation,
}

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Move the type arguments onto the constructor: 'const m = new Map<string, number>();'
  2. Apply the rule's autofix (fixer uses RuleFixer) across the codebase in one commit
  3. If the annotation style is preferred, revert the config to the default 'prefer-type-annotation'

Example fix

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

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

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: oxlint runs with 'constructor-type': 'prefer-constructor-type-arguments' and sees 'const m: Map<string, number> = new Map();' — the annotation carries the generics instead of 'new Map<string, number>()'.

Common situations: Switching the config from the default annotation style to constructor style; onboarding code written by teams whose convention is explicit instantiation; editors auto-adding quick-fix types on assignment.

Related errors


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