Typed Classes
A class bundles data and behavior. In TypeScript each field and method can carry a type for safer code.
- Fields are declared with their types
- The constructor sets initial values
- Methods can declare return types
class Dog {
name: string;
constructor(name: string) {
this.name = name;
}
}
Mark a field readonly when it should never change after the constructor runs.
Try it yourself
Loading editor…