June 2025
Intermediate to advanced
837 pages
24h 50m
English
In addition to the types included in TypeScript, you can also define your own types. Normally, this is done via classes because every class in TypeScript is also a new type that you can use for type markup. TypeScript classes follow the same syntax as JavaScript. Listing 13.22 shows the basic framework of a class and how to use it.
class Movie { rating = 0; constructor( public id: number, public title: string, public year: number) {}}function rate(movie: Movie, rating: number): void { movie.rating = rating;}const ironMan = new Movie(1, 'Iron Man', 2008);rate(ironMan, 4); console.log(ironMan);// Output: Movie { id: 1, title: 'Iron Man', year: 2008, rating: 4 }
Listing 13.22 Classes in TypeScript
The class represents two special ...
Read now
Unlock full access