{"record":{"id":"16614391d6db9cd3","repo":"oxc-project/oxc","slug":"class-cannot-have-method-named-new","errorCode":null,"errorMessage":"Class cannot have method named `new`.","messagePattern":"Class cannot have method named `new`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/typescript/no_misused_new.rs","lineNumber":21,"sourceCode":"    ast::{\n        ClassElement, IdentifierReference, PropertyKey, TSSignature, TSType, TSTypeAnnotation,\n        TSTypeName,\n    },\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_misused_new_interface_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Interfaces cannot be constructed, only classes.\")\n        .with_help(\"Consider removing this method from your interface.\")\n        .with_label(span)\n}\n\nfn no_misused_new_class_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Class cannot have method named `new`.\")\n        .with_help(\"This method name is confusing, consider renaming the method to `constructor`\")\n        .with_label(span)\n}\n\nfn get_return_type_identifier<'a, 'b>(\n    return_type: Option<&'b TSTypeAnnotation<'a>>,\n) -> Option<&'b IdentifierReference<'a>> {\n    if let Some(return_type) = return_type\n        && let TSType::TSTypeReference(type_ref) = &return_type.type_annotation\n        && let TSTypeName::IdentifierReference(id) = &type_ref.type_name\n    {\n        Some(id)\n    } else {\n        None\n    }\n}\n\n#[derive(Debug, Default, Clone)]","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/typescript/no_misused_new.rs#L3-L39","documentation":"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`.","triggerScenarios":"`class Foo { new() { ... } }` - a class MethodDefinition named `new`, regardless of signature.","commonSituations":"Factory-method patterns brought from other languages, typos, code generated from templates that emit `new` as a method name.","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"],"exampleFix":"// before\nclass Foo {\n  new() { return new Foo(); }\n}\n\n// after\nclass Foo {\n  static create() { return new Foo(); }\n}","handlingStrategy":"validation","validationCode":"npx oxlint --deny-warnings .","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["typescript","lint","class","naming"],"backgroundTag":"misused-new","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}