Tslint: [unnecessary-constructor] Incorrectly flagging accessibility-restricting constructors

Created on 7 Feb 2019  路  3Comments  路  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.12.1
  • __TypeScript version__: 3.1.6
  • __Running TSLint via__: Atom

TypeScript code being linted

class SomethingNotConstructible {
    private constructor() {}
}

with tslint.json configuration:

{
    "rules": {
        "unnecessary-constructor": true
    }
}

Actual behavior

The private constructor is flagged as unnecessary.

Expected behavior

The private constructor is not flagged.

There are cases where a class shouldn't be constructible (factory classes or something) where it would make sense to have an empty constructor with the private/etc modifier to prevent it from being constructed externally. Right now, this private empty constructor is flagged by the unnecessary-constructor rule. In my estimation, this constructor _is_ necessary, because it performs a task that would not performed were it not included (makes the constructor inaccessible).

Easy Accepting PRs Bug good first issue

Most helpful comment

hello, i am new to this open source world but have been coding in js and typescript for around 2-3 years now. i can look into this bug and if possible can make necessary changes to it.
so can i start working on it ?

All 3 comments

Agreed, thanks for the catch @ericbf!

This should be a pretty straightforward change: the rule should also check if there's a privacy modifier on the constructor other than public.

hello, i am new to this open source world but have been coding in js and typescript for around 2-3 years now. i can look into this bug and if possible can make necessary changes to it.
so can i start working on it ?

@nirajgeorgian Absolutely

Was this page helpful?
0 / 5 - 0 ratings