Skip to content

Make call to super() implicit within constructor #5910

Closed

Description

@nippur72

Calling super() within a constructor is a bit redundant and annoying... can we have it called implicitly if call to super is omitted?

For example this

class Manager extends Employee {
   constructor() {
      this.title = "The Manager";
   }
}

should be equivalent to

class Manager extends Employee {
   constructor() {
      super(); // call to default constructor added implicitly      
      this.title = "The Manager";
   }
}

(C# also does a similar thing and it's a nice feature).

Activity

  1. coreh commented on Dec 3, 2015

    @coreh

    What would be the default behavior for the parameters? Would they be passed on to the super class constructor if the signature was the same?

  2. nippur72 commented on Dec 3, 2015

    @nippur72
    Author

    Marco Buono (@coreh) that would be a possibility, since you can always override it by making an explicit call to super(). It would do no harm to existing code.

  3. GoToLoop commented on Dec 3, 2015

    @GoToLoop

    Gonna add that like C#, Java does the same too for empty super constructors! :D

  4. mhegazy commented on Dec 3, 2015

    @mhegazy
    Contributor

    TypeScript is and should be a super set of JavaScript ES6 semantics has no implicit call for constructors, if TS supports that, a TS/ES6 class are not identical in behavior.

  5. GoToLoop commented on Dec 3, 2015

    @GoToLoop

    As long as some handy feature isn't detrimental to ES6 compatibilty, I don't see why it should be thrown away.
    This proposal merely asks for TS' compiler to match some of the smartness of C# & Java compilers.
    It won't affect codes which don't use this saving grace feature.

    Since TS is a compiler/transpiler it can do tricks that raw JS can't after all.
    Many C# & Java programmers would be attracted to JS world if TS would mirror many of C#/Java features where it's feasible to do so.

  6. nippur72 commented on Dec 3, 2015

    @nippur72
    Author

    peraphs there could be a compiler flag like #274 to turn on and off features like this one.

  7. nippur72 commented on Dec 4, 2015

    @nippur72
    Author

    @jasonwilliams200OK the new behaviour would be:

    • if there's no super() call and the class has a default constructor, instead of throwing an error, make a call to super() as the first thing in the constructor.
    • else work as before

    Your special case would be covered by calling super explicitly. (And btw, is it possible to refer to this before super?)

  8. RyanCavanaugh commented on Dec 5, 2015

    @RyanCavanaugh
    Member

    What if my intention was to call super() after the this.title assignment in certain use-case.

    Note that this is disallowed in ES6. You cannot access this until after the super call.

  9. added
    SuggestionAn idea for TypeScript
    DeclinedThe issue was declined as something which matches the TypeScript vision
    Out of ScopeThis idea sits outside of the TypeScript language design constraints
    on Feb 19, 2016
  10. mhegazy commented on Feb 19, 2016

    @mhegazy
    Contributor

    As mentioned above, we would like to keep the semantics matching JS, and issue warnings to help guide the user the correct behavior.

  11. locked and limited conversation to collaborators on Jul 31, 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

    DeclinedThe issue was declined as something which matches the TypeScript visionOut of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions