November 2019
Beginner
804 pages
20h 1m
English
One last point that we would like to raise about generics is the fact that you can define generic constraints to narrow down the set of generic types that you accept. This is done by using the extends keyword (for example, T extends SomeType).
This is useful when you only want to allow a specific subset of types to be passed as generic arguments. Thanks to generic constraints, you can be sure that the properties/methods you expect will be available.
The following is an example:
abstract class Recipe {
constructor(public name: string, public ingredients: string[]) {
}
}
class ItalianRecipe extends Recipe {
}
class FrenchRecipe extends Recipe {
constructor(name: string, ingredients: string[], public chef: string) { super(name, ...Read now
Unlock full access