June 2025
Intermediate to advanced
837 pages
24h 50m
English
In addition to interfaces, TypeScript also allows you to define type aliases by using the type keyword. Type aliases are used in a similar way as interfaces, but are somewhat simpler in structure. Listing 13.27 shows that you can implement the example with interfaces in a similar way with type aliases. The biggest difference is that a class can’t implement a type alias.
type Movie = { id: number; title: string; year: number; rating: number; rate(rating: number): void;}; class ActionMovie { public rating = 0; constructor(public id: number, public title: string, public year: number) {} rate(rating: number) { this.rating = rating; }} function show(movie: Movie) { console.log('Now showing: ', movie.title);}
Read now
Unlock full access