Typing effector
Best practices for writing well-typed code
createEvent#
By default, this method returns Event<void>.
Event type can be defined as generic
createEffect#
Typescript can infer effect result type from given handler, but argument type should be defined either in handler argument or as generic type
createEffect and custom errors#
When you need custom error types (Fail type in Effect) you can define all generics explicitly:
In case when effect's handler is defined before effect itself you can allow typescript to infer type of Params and Done by using typeof handler in first generic and optionally provide Fail type as second one
since
effector 21.6.0
event.prepend#
To add types to events, created by event.prepend you need to add type either to prepend function argument or as generic type
attach#
To allow typescript to infer types of created effect, add type to mapParams first argument, which will become effect params type
split#
Typescript type predicates can be used to split common event type to several cases (hence the name)
guard#
Typescript type predicates can be used to infer result type by filter function
createApi#
To allow typescript to infer types of created events, add type to second argument of given reducers
is#
is methods can help to infer unit type (thereby is methods acts as TypeScript type guards) which can help to write strongly-typed helper functions