Strongly Typing the Code
Understand how to enforce strong typing in Angular applications using TypeScript interfaces. This lesson guides you through typing NgRx services, actions, reducers, and selectors to improve code safety and maintainability.
Since we’ve already defined the appropriate interfaces, let’s strongly type our code using them.
Strongly typing the services
In our application, we have two services—DataService and ProductsService. In this lesson, we’ll strongly type these services one by one.
Strongly typing the DataService class
Since the products variable in line 13 and the shoppingCart variable in line 81 contain an array of products, let’s specify their type to be Product[] instead of any.
Strongly typing the ProductsService class
Let’s strongly type the ProductsService class by utilizing the Product interface. The highlighted lines are those in which we replace the keyword any with the Product interface.
Strongly typing the ProductsModule
Let’s strongly type the NgRx actions, reducers, and selectors of the ProductsModule.
Action
When a user visits the home page of our ...