|
|
|
|
|
by undecidabot
2476 days ago
|
|
Looks like another biased comparison to me. You could easily rewrite the TypeScript reducer example (which fails to compile btw) to be similarly concise while being as safe. interface State {
movies: string[]
}
// you may also use { tag: "Action", payload: string }
// if you prefer something more structured
type Action =
| ["AddMovie", string]
| ["RemoveMovie", string]
| ["Reset"]
const defaultState: State = { movies: [] }
const reducer = (state: State, action: Action): State => {
switch (action[0]) {
case "AddMovie": return { movies: [action[1], ...state.movies] }
case "RemoveMovie": return { movies: state.movies.filter(m => m !== action[1]) }
case "Reset": return defaultState
}
}
const someAction: Action = ["AddMovie", "The End of Evangelion"]
TypesScript's type system is actually very flexible and advanced [1] compared to most type systems since it had to adapt to the very "dynamic" structuring of JavaScript in the wild.[1] https://www.typescriptlang.org/docs/handbook/advanced-types.... |
|
I wouldn't want to work on a project where everyone was creating object literals and passing them to dispatch everywhere.