Understanding classes and interfaces
We've already briefly looked at classes and interfaces in previous sections. Let's take a deeper look, in this section, and see why these types can help us write better code. Once we complete this section, we will be better prepared to write more readable, reusable code with fewer bugs.
Classes
At a base level, classes in TypeScript look just like classes in JavaScript. They are a container for a related set of fields and methods that can be instantiated and reused. However, classes in TypeScript support extra features for encapsulation that JavaScript does not. Let's take a look at an example.
Create a new file called classes.ts
and enter the following code:
class Person { constructor() {} msg: string; speak() { console.log(this.msg); } } const tom = new Person(); tom.msg = "...