Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-useless-constructor] constructor with super(someArgs) is not useless in Angular #1164

Closed
JounQin opened this issue Oct 31, 2019 · 7 comments
Labels
awaiting response Issues waiting for a reply from the OP or another party package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@JounQin
Copy link
Contributor

JounQin commented Oct 31, 2019

Repro

{
  "rules": {
    "@typescript-eslint/no-useless-constructor": 2
  }
}
class A {
  protected http: HttpClient
  constructor(protected injector: Injector) {
    this.http = this.injector.get(HttpClient)
  }
}

class B implements A {
  // this should not be considered as useless due to DI
  constructor(injector: Injector) {
    super(injector)
  }
}

Expected Result

No error report

Actual Result

Error

Versions

package version
@typescript-eslint/eslint-plugin 2.6.0
@typescript-eslint/parser 2.6.0
TypeScript 3.6.4
ESLint 6.6.0
node 13.0.1
npm 6.12.0
@JounQin JounQin added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Oct 31, 2019
@JounQin
Copy link
Contributor Author

JounQin commented Oct 31, 2019

We can use following workaround temporarily:

class B implments A {
  constructor(protected injector: Injector) {
    super(injector);
  }
}

But if A declare it as private, we can only change it to another property name like:

class A {
  private http: HttpClient
  constructor(private injector: Injector) {
    this.http = this.injector.get(HttpClient)
  }
}

class B implements A {
  constructor(private _injector: Injector) {
    super(_injector)
  }
}

@bradzacher
Copy link
Member

bradzacher commented Oct 31, 2019

Why do you keep writing class B implements A?
That's not valid code?


Could you please explain why you need the constructor on the child class?

Unless I'm mistaken, this is perfectly valid code, which does exactly the same thing, without the useless constructor:

class A {
  protected http: HttpClient
  constructor(protected injector: Injector) {
    this.http = this.injector.get(HttpClient)
  }
}

class B extends A { }

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for maintainers to take a look labels Oct 31, 2019
@JounQin
Copy link
Contributor Author

JounQin commented Oct 31, 2019

Oh sorry, it should be extends, it's a typo.

B's constructor is required in favor of DI system in Angular.

@bradzacher
Copy link
Member

Have you got some docs on why the constructor is explicitly needed on the child?
I haven't used angular since like 1.6, so I don't know how it works.

Unless I'm mistaken it shouldn't need it explicitly, as the child should implicitly inherit the parent's constructor.

@JounQin
Copy link
Contributor Author

JounQin commented Nov 1, 2019

@bradzacher Just take https://stackblitz.com/edit/angular-gh3sf4 as an example.

@bradzacher
Copy link
Member

Please be careful with your @ tagging. You tagged the wrong account.
Github has a crappy implementation, and doesn't put my name first in the autocomplete list.


Looking into this, I have no idea why it's required. I think angular statically analyses the file by itself? I'm not sure, the custom compiler is a black box to me.

We don't want to encode the semantics of angular into typescript.
The fact that the constructor is required by the angular compiler is an implementation detail of the framework.

If you'd like this functionality, I'd suggest getting a rule created in a plugin like eslint-plugin-angular.

@JounQin
Copy link
Contributor Author

JounQin commented Nov 1, 2019

@bradzacher Thx for your patients. I will open a feature request issue at angular-eslint then.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting response Issues waiting for a reply from the OP or another party package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants