Reputation: 18465
All the examples I see show a class with a constructor. Is it ok to not put a constructor in? a lot like how C# automatically makes you a default empty constructor?
Upvotes: 36
Views: 21356
Reputation: 221212
From the spec, section 8.3 (8.3):
A class may contain at most one constructor declaration. If a class contains no constructor declaration, an automatic constructor is provided, as described in section 8.3.3. (8.3.3.)
Upvotes: 49
Reputation: 181
Just to extend the accepted answer and correct an answer by Jon Gear (TS might have changed in the meantime):
Derived class does not need to create a constructor with the sole purpose to call super()
.
https://stackblitz.com/edit/no-need-for-derived-constructor?file=index.ts
Upvotes: 12
Reputation: 1018
Correct. Classes in TypeScript do not require you to explicitly write a constructor. However if you are extending a base class you will need to create a constructor to call super() at a minimum.
Upvotes: 17