Typing effector
Best practices for writing well-typed code
#
createEventBy default, this method returns Event<void>
.
Event type can be defined as generic
#
createEffectTypescript 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 errorsWhen 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.prependTo add types to events, created by event.prepend you need to add type either to prepend function argument or as generic type
#
attachTo allow typescript to infer types of created effect, add type to mapParams
first argument, which will become effect params
type
#
splitTypescript type predicates can be used to split common event type to several cases (hence the name)
#
guardTypescript type predicates can be used to infer result type by filter
function
#
createApiTo allow typescript to infer types of created events, add type to second argument of given reducers
#
isis
methods can help to infer unit type (thereby is
methods acts as TypeScript type guards) which can help to write strongly-typed helper functions