Typescript
Overmind is written in Typescript and it is written with a focus on you dedicating as little time as possible to help Typescript understand what your app is all about. Typescript will spend a lot more time helping you. If you are not a Typescript developer Overmind is a really great project to start learning it as you will get the most out of the little typing you have to do.
Configuration
First we need to define the typing of our configuration and there are two approaches to that.
1. Declare module
The most straightforward way to type your application is to use the declare module approach. This will work for most applications, but might make you feel uncomfortable as a hardcore Typescripter. The reason is that we are overriding an internal type, meaning that you can only have one instance of Overmind running inside your application.
import { IConfig } from 'overmind'
const config = {}
declare module 'overmind' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Config extends IConfig<{
state: typeof config.state,
actions: typeof config.actions,
effects: typeof config.effects
}> {}
// Due to circular typing we have to define an
// explicit typing of state, actions and effects since
// TS 3.9
}Now you can import any type directly from Overmind and it will understand the configuration of your application. Even the operators are typed.
2. Explicit typing
You can also explicitly type your application. This gives more flexibility.
You only have to set up these types once, where you bring your configuration together. That means if you use multiple namespaced configuration you still only create one set of types, as shown above.
Now you only have to make sure that you import your types from this file, instead of directly from the Overmind package.
State
The state you define in Overmind is just an object where you type that object.
When writing Typescript you should not use optional values for your state (?), or use undefined in a union type. In a serializable state store world null is the value indicating “there is no value”.
Getter
Derived
Note that the type argument you pass is the object the derived is attached to, so with nested derived:
Note that with Explicit Typing you need to also pass the a third argument to the derived function, the Config type created in your main index.ts file.
Statemachine
A statemachine takes a type of states. A big benefit of this approach is that you can type what state is available in any transition state. So for example, you will only have a user if you are in the authenticated transition state.
Now whenever you access this state you can check the current transition state and doing so get the correct state back:
When doing state transitions in actions your will receive the statemachine as the first argument to both entry and exit callbacks, giving you the correct typing.
Actions
The action type takes either an input type, an output type, or both.
You also have an async version of this type. You use this when you want to define an async function, which implicitly returns a promise, or use it on a function that explicitly returns a promise.
Effects
There are no Overmind specific types related to effects, you just type them in general.
Operators
Operators is like the Action type: it can take an optional input, but it always produces an output. By default the output of an operator is the same as the input.
The Operator type is used to type all operators. The type arguments you give to Operator have to match the specific operator you use though. So for example if you type a mutate operator with a different output than the input:
Typescript yells at you, because this operator just passes the value straight through.
Typically you do not think about this and Typescript rather yells at you when the value you are passing through your operators is not matching up.
Generic input
You might create an operator that does not care about its input. For example:
Composing doSomething into the pipe gives an error, cause the action is typed with a string input, but the doSomething operator is typed with void.
To fix this we just add a generic type to the definition of our operator:
Now Typescript infers the input type of the operator and passes it along.
Partial input
For example:
Now the input is actually okay, because { isAwesome: boolean } matches the User type, but we are also now saying that the type of output will be { isAwesome: boolean }, which does not match the User type required by handleAwesomeUser.
To fix this we again infer the type, but using extends to indicate that we do have a requirement to the type it should pass through:
That means this operator can handle any type that matches an isAwesome property, though will pass the original type through.
Last updated