Skip to content

Warning on no constructor and super class constructor taking parameters #6979

Closed

Description

Consider this case:

class Parent {
    constructor(requiredArg) {}
}
class Child extends Parent {}

This currently doesn't produce an error. I believe it should.

Activity

  1. RyanCavanaugh commented on Feb 9, 2016

    @RyanCavanaugh
    Member

    Note that you can't just write

    let x = new Child();

    And that the generated code calls super for you:

    var Child = (function (_super) {
        __extends(Child, _super);
        function Child() {
            _super.apply(this, arguments); <---
        }
        return Child;
    })(Parent);

    Based on reading the linked issue, it sounds like you're depending on a DI framework that reflects over the code to figure out what parameters to pass a constructor. In this case, it's up to you to reflect that behavior in your class one way or another (is static $inject = [...] available to you?).

    After shipping for three years with the desirable behavior of "Derived classes with no declared constructors automatically get their base class's constructors", it's not feasible to break everyone and tell them to go manually rewrite their constructors so that users of particular DI frameworks get better warnings in some cases.

    It would be very easy to write a no-implicit-constructors TSLint rule if that's the kind of thing you wanted to enforce in your project.

  2. staeke commented on Feb 9, 2016

    @staeke
    Author

    Sorry....my bad. This didn't make sense at all. Of course it shouldn't. Full story:

    This comes from an Angular 2 use case. The Parent has some decorators for dependency injection. These aren't picked up by Angular since they aren't exposed by Child. The (ES5) javascript generated is

    _super.apply(this, arguments)
    

    ...which works fine invocation wise, but not decoration wise.

  3. staeke commented on Feb 9, 2016

    @staeke
    Author

    Ryan Cavanaugh (@RyanCavanaugh) you're so right! Well guessed!

  4. staeke commented on Feb 9, 2016

    @staeke
    Author

    Ryan Cavanaugh (@RyanCavanaugh) I think a better question would be: can we open up for Angular (and similar) to deduce in runtime the constructor hierarchy and look for decoration metadata in the parent. This currently seems impossible (unless I'm mistaken)

  5. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions