question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow subclass constructors without super() call

See original GitHub issue

TypeScript Version:

1.8.0

Code

Custom element classes often have constructors that just return document.createElement(), since HTMLElement doesn’t have a callable constructor, but this generates an error in TypeScript:

class MyElement extends HTMLElement {
  constructor() {
    return document.createElement('my-element');
  }
}

In general, it is completely valid ES2015 to not call super(), as long as you don’t access this.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:21 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
justinfagnanicommented, Feb 29, 2016

Apparently TS’s super() check isn’t too robust, so I just did this:

constructor() {
  if (1 < 0) { // edit, this used to be 1 < 0, @xLama is right :)
    super();
  }
  return document.createElement('my-element');
}
1reaction
DanielRosenwassercommented, Jul 19, 2016

Given that HTMLElement’s constructor never actually returns, it should actually return never. I’m thinking that

  1. It should be allowed to extend something which has a construct signature of type never.
  2. If a construct signature of some value’s type returns never, then a super call is not needed if extending from that value.
  3. If there is a super() call, it should be an error to access this before super (which is currently the case anyway).
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to force my subclass constructor not to call base class ...
If the super class does not have a no-argument constructor, you will get a compile-time error.
Read more >
Using the Keyword super (The Java™ Tutorials > Learning ...
With super() , the superclass no-argument constructor is called. With super(parameter list) , the superclass constructor with a matching parameter list is ...
Read more >
9.2. Inheritance and Constructors — AP CSAwesome
When a subclass's constructor does not explicitly call a superclass's constructor using super, Java inserts a call to the superclass's no-argument constructor.
Read more >
Inheritance | Kotlin Documentation
When designing a base class, you should therefore avoid using open members in the constructors, property initializers, or init blocks. Calling the superclass...
Read more >
Design Subclass Constructors - MATLAB & Simulink
Explicitly calling each superclass constructor from a subclass constructor enables you to: ... If you do not explicitly call the superclass constructors from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found