oxc-project/oxc · warning · OxcDiagnostic
Class cannot have method named `new`.
Error message
Class cannot have method named `new`.
What it means
Companion diagnostic of no-misused-new for classes: a method literally named `new` on a class is not a constructor, and the name misleads readers. The help at no_misused_new.rs:22-26 recommends renaming it to `constructor`.
Source
Thrown at crates/oxc_linter/src/rules/typescript/no_misused_new.rs:21
ast::{
ClassElement, IdentifierReference, PropertyKey, TSSignature, TSType, TSTypeAnnotation,
TSTypeName,
},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use crate::{AstNode, context::LintContext, rule::Rule};
fn no_misused_new_interface_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Interfaces cannot be constructed, only classes.")
.with_help("Consider removing this method from your interface.")
.with_label(span)
}
fn no_misused_new_class_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Class cannot have method named `new`.")
.with_help("This method name is confusing, consider renaming the method to `constructor`")
.with_label(span)
}
fn get_return_type_identifier<'a, 'b>(
return_type: Option<&'b TSTypeAnnotation<'a>>,
) -> Option<&'b IdentifierReference<'a>> {
if let Some(return_type) = return_type
&& let TSType::TSTypeReference(type_ref) = &return_type.type_annotation
&& let TSTypeName::IdentifierReference(id) = &type_ref.type_name
{
Some(id)
} else {
None
}
}
#[derive(Debug, Default, Clone)]View on GitHub (pinned to e1e7af627c)
Solutions
- Rename the method to `constructor` if it was meant to construct
- Rename it to `create` or `make` if it is an intentional factory method
- Delete it if it is dead scaffolding
Example fix
// before
class Foo {
new() { return new Foo(); }
}
// after
class Foo {
static create() { return new Foo(); }
} Defensive patterns
Strategy: validation
Validate before calling
npx oxlint --deny-warnings .
Prevention
- Reserve the name `new` for nothing in class bodies; use `constructor` or `create`
- Grep for `new(` and `new (` inside class bodies when adopting the rule
- Add an ast-grep or lint check in CI to catch the pattern in generated code
When it happens
Trigger: `class Foo { new() { ... } }` - a class MethodDefinition named `new`, regardless of signature.
Common situations: Factory-method patterns brought from other languages, typos, code generated from templates that emit `new` as a method name.
Related errors
- Literals should be exposed using readonly fields.
- Literals should be exposed using getters.
- Missing accessibility modifier on {member_type} {name}.
- Public accessibility modifier on {member_type} {name}.
- Unexpected empty class.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/16614391d6db9cd3.
Report an issue: GitHub.