
We enforce the structure that the event argument of PizzaMaker.create() takes whilst still being able to create the object that the type Pizza as a class defines! We get the best of both worlds here - the blueprint and the contract. Understanding what we can get from each structure will easily let us make the best decision that will enhance our code and improve our developer experience.Ĭlass Pizza The decision to use a class or an interface truly depends on our use case: type-checking only, implementation details (typically via creating a new instance), or even both! We can use classes for type-checking and the underlying implementation - whereas we cannot with an interface. Since both of these structures define what an object looks like, both can be used in TypeScript to type our variables. Once you’re finished, check out my other article on TypeScript Interfaces vs Types!

An interface is a group of related properties and methods that describe an object, but neither provides implementation nor initialisation for them. A class is a blueprint from which we can create objects that share the same configuration - properties and methods. Classes and interfaces are powerful structures that facilitate not just object-oriented programming but also type-checking in TypeScript.
