Este producto no es compatible con el sitio Datadog seleccionado. ().
Esta página aún no está disponible en español. Estamos trabajando en su traducción.
Si tienes alguna pregunta o comentario sobre nuestro actual proyecto de traducción, no dudes en ponerte en contacto con nosotros.

Metadata

ID: typescript-best-practices/no-useless-constructor

Language: TypeScript

Severity: Warning

Category: Best Practices

Description

This rule is designed to flag constructors that either do nothing or only call the super function. These constructors are unnecessary and can be safely removed. In JavaScript, if a class extends another class and does not have a constructor, it automatically calls the super function with all the arguments it receives.

Unnecessary constructors can lead to confusion for other developers who may be reading or maintaining your code. They might spend time trying to figure out why a constructor is there when it doesn’t need to be, or they might assume that the constructor is doing something important when it’s not. To follow this rule and write good, clean code, you should only write a constructor if it’s doing something other than just calling super.

Non-Compliant Code Examples

class One {
    constructor() {}
}

class Two extends One {
    constructor(...args) {
      super(...args);
    }
}

class Three {
    constructor(arg) {}
}

Compliant Code Examples

class One {}

class Two {
    constructor() {
        doSomething();
    }
}

class Three extends One {
    constructor() {
        super('hello world');
    }
}

class Four extends One {
    constructor() {
        super();
        doSomething();
    }
}

class Five {
    constructor(private arg) {}
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Integraciones sin problemas. Prueba Datadog Code Security