redux-saga-routines icon indicating copy to clipboard operation
redux-saga-routines copied to clipboard

createRoutineCreator - how to use with TypeScript?

Open akhalinem opened this issue 4 years ago • 13 comments

akhalinem avatar Jul 26 '20 18:07 akhalinem

+1

image

createRoutineCreator and defaultRoutineStages aren't "available" in @types/redux-saga-routines module

Necessary if we want to create new routines that are strongly-typed

talor-hammond avatar Oct 19 '20 04:10 talor-hammond

@Abror1997 here's a solution i came up with; it's not perfect

Custom routine creator with strong typing image

Custom routine creator type image

talor-hammond avatar Oct 19 '20 05:10 talor-hammond

Why not to submit a PR for types? :)

alexey-pelykh avatar Dec 30 '20 04:12 alexey-pelykh

@alexey-pelykh I guess that types are already in the DefinitelyTyped repo or in progress.

shapkarin avatar Dec 31 '20 04:12 shapkarin

@alexey-pelykh I think that somewhere types are already declaraded. Please, make sure.

shapkarin avatar Dec 31 '20 04:12 shapkarin

+1 Also keen to use this with typescript

JessicaBunyan avatar Jan 14 '21 22:01 JessicaBunyan

@JessicaBunyan you'll need @types/redux-saga-routines

talor-hammond avatar Jan 14 '21 22:01 talor-hammond

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

JessicaBunyan avatar Jan 14 '21 22:01 JessicaBunyan

image

createRoutineCreator and defaultRoutineStages 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 avatar Jan 15 '21 03:01 talor-hammond

@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

shapkarin avatar Jan 15 '21 05:01 shapkarin

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

shapkarin avatar Jan 15 '21 10:01 shapkarin

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
  );
};

tshallenberger avatar Oct 09 '21 20:10 tshallenberger

Hello. Maybe you can just make a PR to the https://github.com/DefinitelyTyped/DefinitelyTyped monorepo?

shapkarin avatar Oct 12 '21 07:10 shapkarin