Fonts and Layout for Global Scripts
simoncozens.github.io1 pointsby undecidabot0 comments
export const addMovie = (movie: string): Action => ({ tag: "AddMovie", movie });
What's the issue with passing object literals? I know it looks a bit hacky and messy, but if it's typesafe (which it is) then it doesn't seem like a real issue. 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://z.ai/blog/glm-5.2