redux-saga-routines
redux-saga-routines copied to clipboard
createRoutineCreator - how to use with TypeScript?
+1
createRoutineCreator
and defaultRoutineStages
aren't "available" in @types/redux-saga-routines module
Necessary if we want to create new routines that are strongly-typed
@Abror1997 here's a solution i came up with; it's not perfect
Custom routine creator with strong typing
Custom routine creator type
Why not to submit a PR for types? :)
@alexey-pelykh I guess that types are already in the DefinitelyTyped
repo or in progress.
@alexey-pelykh I think that somewhere types are already declaraded. Please, make sure.
+1 Also keen to use this with typescript
@JessicaBunyan you'll need @types/redux-saga-routines
npm install @types/redux-saga-routines@latest
TypeScript error in [redacted](2,43):
Module '"../../../../node_modules/@types/redux-saga-routines"' has no exported member 'createRoutineCreator'. TS2305
createRoutineCreator
anddefaultRoutineStages
aren't "available" in @types/redux-saga-routines module
@JessicaBunyan just remembered, these two^ don't have type declarations in the @types package ): I've had to tell ts-lint not to complain about it -- see the first image
@talor-hammond I remember createRoutineCreator is related to my PR https://github.com/afitiskin/redux-saga-routines/pull/59 :-) maybe I will take some time to add types, I also need them for the own project
If somebody can make that much faster, please feel free. Needed to provide a PR with types to the @types/redux-saga-routines
repository, currently needed files are there – https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/redux-saga-routines:
- types for the
createRoutineCreator
- and for the
defaultRoutineStages
Really needed this today as well. Can't add the types atm but I typed up the gist from the screenshot posted above:
https://gist.github.com/valkn0t/26099bb8e90f90f3d5bd2c7da88abe91
import {
createRoutine,
// @ts-ignore
createRoutineCreator,
// @ts-ignore
defaultRoutineStages,
ResolveActionCreatorByPayload,
Routine,
} from "redux-saga-routines";
export interface RoutinePayloadCreator<
TTriggerPayloadCreator,
TRequestPayloadCreator,
TSuccessPayloadCreator,
TFailurePayloadCreator,
TFulfillPayloadCreator
> {
TRIGGER?: TTriggerPayloadCreator;
trigger?: TTriggerPayloadCreator;
REQUEST?: TRequestPayloadCreator;
request?: TRequestPayloadCreator;
SUCCESS?: TSuccessPayloadCreator;
success?: TSuccessPayloadCreator;
FAILURE?: TFailurePayloadCreator;
failure?: TFailurePayloadCreator;
FULFILL?: TFulfillPayloadCreator;
fulfill?: TFulfillPayloadCreator;
}
type CacheableRoutine<
TTriggerPayloadCreator,
TRequestPayloadCreator,
TSuccessPayloadCreator,
TFailurePayloadCreator,
TFulfillPayloadCreator
> = Routine<
ResolveActionCreatorByPayload<TTriggerPayloadCreator>,
ResolveActionCreatorByPayload<TRequestPayloadCreator>,
ResolveActionCreatorByPayload<TSuccessPayloadCreator>,
ResolveActionCreatorByPayload<TFailurePayloadCreator>,
ResolveActionCreatorByPayload<TFulfillPayloadCreator>
> & { invalidate: ResolveActionCreatorByPayload<TTriggerPayloadCreator> };
export const createCacheableRoutine = <
TTriggerPayloadCreator,
TRequestPayloadCreator,
TSuccessPayloadCreator,
TFailurePayloadCreator,
TFulfillPayloadCreator
>(
action: string,
payloadCreator: RoutinePayloadCreator<
TTriggerPayloadCreator,
TRequestPayloadCreator,
TSuccessPayloadCreator,
TFailurePayloadCreator,
TFulfillPayloadCreator
>
): CacheableRoutine<
TTriggerPayloadCreator,
TRequestPayloadCreator,
TSuccessPayloadCreator,
TFailurePayloadCreator,
TFulfillPayloadCreator
> => {
const routineStages = [...defaultRoutineStages, "INVALIDATE"];
return createRoutineCreator(routineStages)(
`${action}-${JSON.stringify(payloadCreator.trigger)}`,
payloadCreator
);
};
Hello. Maybe you can just make a PR to the https://github.com/DefinitelyTyped/DefinitelyTyped monorepo?