Hone logo
Hone
Problems

Implementing Typed Actions with NGRX in Angular

This challenge focuses on enhancing the maintainability and type safety of your Angular application by implementing typed actions using NGRX. Typed actions provide compile-time checking of action types, reducing runtime errors and improving developer experience. You'll be building a simple counter application and refactoring its actions to be strongly typed.

Problem Description

You are tasked with creating a counter application using Angular and NGRX. The application should have a counter value that can be incremented and decremented. Currently, the actions are defined using string literals, which can lead to errors if the action types are misspelled. Your goal is to refactor the actions to use typed actions, ensuring type safety and improved code clarity.

What needs to be achieved:

  1. Create a basic Angular application with NGRX.
  2. Define a counter feature module with a reducer, selector, and initial state.
  3. Implement actions for incrementing and decrementing the counter.
  4. Refactor the actions to use typed actions from @ngrx/store.
  5. Ensure the reducer and selector are updated to work with the typed actions.

Key Requirements:

  • Use @ngrx/store and @ngrx/effects for state management.
  • Implement a counter feature module.
  • Define increment and decrement actions.
  • Use createAction from @ngrx/store to create typed actions.
  • Update the reducer and selector to handle the typed actions.
  • The application should function correctly after the refactoring.

Expected Behavior:

  • The application should display the current counter value.
  • Clicking the "Increment" button should increase the counter value by 1.
  • Clicking the "Decrement" button should decrease the counter value by 1.
  • The application should not throw any errors related to action types.
  • The TypeScript compiler should provide type checking for the actions.

Edge Cases to Consider:

  • Initial counter value of 0.
  • Decrementing the counter below 0 (consider whether to allow negative values or not - the default behavior should be to prevent going below 0).
  • Handling potential errors during action dispatching (though this is less critical for this specific challenge).

Examples

Example 1:

Input: Initial counter value: 0, Increment button clicked once.
Output: Counter value: 1
Explanation: The increment action is dispatched, and the reducer updates the state to increment the counter by 1.

Example 2:

Input: Initial counter value: 5, Decrement button clicked twice.
Output: Counter value: 3
Explanation: The decrement action is dispatched twice, and the reducer updates the state to decrement the counter by 2.

Example 3:

Input: Initial counter value: 0, Decrement button clicked once.
Output: Counter value: 0
Explanation: The decrement action is dispatched, but the counter value is prevented from going below 0.

Constraints

  • The application should be written in TypeScript.
  • Use Angular version 16 or higher.
  • Use NGRX version 16 or higher.
  • The solution should be well-structured and maintainable.
  • Focus on the core task of implementing typed actions; complex UI features beyond increment/decrement are not required.
  • The counter value should not go below 0.

Notes

  • Consider using createAction from @ngrx/store to define your actions with type safety.
  • The reducer should use a switch statement or similar mechanism to handle different action types.
  • The selector should retrieve the current counter value from the state.
  • Think about how to update your existing code to use the new typed actions. A gradual refactoring approach is often best.
  • Pay close attention to the TypeScript compiler errors – they will guide you towards a correct solution.
Loading editor...
typescript