Allow subclass constructors without super() call
See original GitHub issueTypeScript 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:
- Created 8 years ago
- Comments:21 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Apparently TS’s super() check isn’t too robust, so I just did this:
Given that
HTMLElement
’s constructor never actually returns, it should actually returnnever
. I’m thinking thatnever
.never
, then asuper
call is not needed if extending from that value.super()
call, it should be an error to accessthis
beforesuper
(which is currently the case anyway).