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.
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.
The Overmind documentation is written for implicit typing. That means whenever you see a type import directly from the Overmind package, you should rather import from your own defined types.
State
The state you define in Overmind is just an object where you type that object.
It is important that you use a type and not an interface. This has to do with the way Overmind resolves the state typing.
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”.
typeState= {// Do not do this foo?:string// Do not do this foo:string|undefined// Do this foo:string|null// Or this, if there always will be a value there foo:string}exportconststate:State= { foo:null}
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.
Actions
The action type takes either an input type, an output type, or both.
import { Action } from'overmind'exportconstnoArgAction:Action= (context, value) => { value // this becomes "void"}exportconstargAction:Action<string> = (context, value) => { value // this becomes "string"}exportconstnoArgWithReturnTypeAction:Action<void,string> = (context, value) => { value // this becomes "void"return'foo'}exportconstargWithReturnTypeAction:Action<string,string> = (context, value) => { value // this becomes "string"return value +'!!!'}
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.
import { AsyncAction } from'overmind'exportconstnoArgAction:AsyncAction=async (context, value) => { value // this becomes "void"}exportconstargAction:AsyncAction<string> =async (context, value) => { value // this becomes "string"}exportconstnoArgWithReturnTypeAction:AsyncAction<void,string> =async (context, value) => { value // this becomes "void"return'foo'} // returns Promise<string>exportconstargWithReturnTypeAction:AsyncAction<string,string> = (context, value) => { value // this becomes "string"returnPromise.resolve(value +'!!!')} // returns Promise<string>
Effects
There are no Overmind specific types related to effects, you just type them in general.
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.
import { Operator, mutate, filter, map } from'overmind'// You do not need to define any types, which means it defaults// its input and output to "void"exportconstchangeSomeState: () =>Operator= () =>mutate(functionchangeSomeState({ state }) {state.foo ='bar' })// The second type argument is not set, but will default to "User"// The output is the same as the inputexportconstfilterAwesomeUser: () =>Operator<User> = () =>filter(functionfilterAwesomeUser(_, user) {returnuser.isAwesome })// "map" produces a new output so we define that as the second// type argumentexportconsttoNumber: () =>Operator<string,number> = () =>map(functiontoNumber(_, value) { returnNumber(value) })
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:
import { Operator, pipe, action } from'overmind'import*as o from'./operators'import { User } from'./state'exportconstclickedUser:Operator<User> =pipe(o.filterAwesome(),o.handleAwesomeUser())
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:
The void type just defines that there are no nested charts. All the states and points of inserting an action name is now typed. Also the condition callback is typed. Even the matches API is typed correctly.