Reduce Todo Actions from a Typed Union
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Export a Todo type (a numeric id, a text and a done flag) and an Action union told apart by type: add (with an id and a text), toggle (with an id), remove (with an id) and clear (nothing else). Write reducer(todos, action), which returns a new list: add appends a todo that isn't done, toggle flips one todo's done flag, remove drops it and clear removes every done todo. Never change the list passed in. Type Action so an unknown type, a missing field or a field from another action is a type error.
Your code must pass
- Adds and toggles todos
- Removes a todo and clears done ones
- Returns a new list without changing the old one
- Has no type errors
- Action is a union told apart by type
- Rejects an action with the wrong fields
- Rejects an unknown action type