A benefit of a strong type system is the ability to have good guard rails that tell you when you’ve writing an invalid program, without having to run it. However, to be truly useful it needs to be possible and ideally easy to define interfaces and method signatures once, and simply transform these for their various applications.
redux applications you define actions, and these are dispatched to mutate state or trigger side effects. I’m just using this as a way of introducing the TypeScript example, rather than suggesting what follows is a good way of solving this problem (e.g., libraries like Typesafe Actions exists to solve this problem specifically).
- An action object (
AO), which is typically a discriminator union, that encapsulates the information about the action
- An action creator, which is a function that creates the action object.
(...args) => AO
- Then we
connectour components to
redux, which provides bound calls using the arguments of the action creator but which actually returns
Lets assume defining an
interface for the action object and the action creator function are uncontroversial.
How do we easily type the dispatch version of the
performAction function such that we can reuse the function signature that we’ve already defined?
My first pass was to use the
The problem here is that it also gets the return type of
AO, which, while not being a show stopper, doesn’t correctly represent the function.
Upon reading this summary of typescript utility types it finally became clear how to reuse the arguments without bringing in the whole function definition.