typesafe-actions

  • Version 5.1.0
  • Published
  • 296 kB
  • No dependencies
  • MIT license

Install

npm i typesafe-actions
yarn add typesafe-actions
pnpm add typesafe-actions

Overview

Typesafe Action Creators for Redux / Flux Architectures (in TypeScript)

Index

Variables

variable deprecated

const deprecated: {
createAction: typeof createAction;
createCustomAction: typeof createCustomAction;
createStandardAction: typeof createStandardAction;
};

    Functions

    function action

    action: {
    <T extends string, E>(type: T, payload: undefined, meta: undefined, error: E): {
    type: T;
    error: E;
    };
    <T extends string, M, E>(type: T, payload: undefined, meta: M, error: E): {
    type: T;
    meta: M;
    error: E;
    };
    <T extends string, P, E>(type: T, payload: P, meta: undefined, error: E): {
    type: T;
    payload: P;
    error: E;
    };
    <T extends string, P, M, E>(type: T, payload: P, meta: M, error: E): {
    type: T;
    payload: P;
    meta: M;
    error: E;
    };
    <T extends string, M>(type: T, payload: undefined, meta: M): {
    type: T;
    meta: M;
    };
    <T extends string, P, M>(type: T, payload: P, meta: M): {
    type: T;
    payload: P;
    meta: M;
    };
    <T extends string, P>(type: T, payload: P): { type: T; payload: P };
    <T extends string>(type: T): { type: T };
    };

      function createAction

      createAction: {
      <TType extends string>(type: TType): <
      TPayload = undefined,
      TMeta = undefined
      >() => ActionCreatorBuilder<TType, TPayload, TMeta>;
      <
      TType extends string,
      TCreatorPayload extends unknown = undefined,
      TCreatorMeta extends unknown = undefined,
      TArgs extends any[] = any[]
      >(
      type: TType,
      payloadCreator: (...args: TArgs) => TCreatorPayload,
      metaCreator?: (...args: TArgs) => TCreatorMeta
      ): <
      TPayload extends TCreatorPayload = TCreatorPayload,
      TMeta extends TCreatorMeta = TCreatorMeta
      >() => (...args: TArgs) => ActionBuilder<TType, TPayload, TMeta>;
      };

        function createAsyncAction

        createAsyncAction: {
        <
        TType1 extends string,
        TType2 extends string,
        TType3 extends string,
        TType4 extends string = never
        >(
        requestArg: TType1,
        successArg: TType2,
        failureArg: TType3,
        cancelArg?: TType4
        ): AsyncAction<
        TType1,
        unknown,
        unknown,
        never,
        TType2,
        unknown,
        unknown,
        never,
        TType3,
        unknown,
        unknown,
        never,
        TType4,
        unknown,
        unknown,
        never
        >;
        <
        TType1 extends string,
        TType2 extends string,
        TType3 extends string,
        TType4 extends string = never,
        TPayloadCreator1 extends (...args: TArgs1) => TPayload1 = undefined,
        TPayloadCreator2 extends (...args: TArgs2) => TPayload2 = undefined,
        TPayloadCreator3 extends (...args: TArgs3) => TPayload3 = undefined,
        TPayloadCreator4 extends (...args: TArgs4) => TPayload4 = undefined,
        TMetaCreator1 extends (...args: TArgs1) => TMeta1 = undefined,
        TMetaCreator2 extends (...args: TArgs2) => TMeta2 = undefined,
        TMetaCreator3 extends (...args: TArgs3) => TMeta3 = undefined,
        TMetaCreator4 extends (...args: TArgs4) => TMeta4 = undefined,
        TPayload1 extends unknown = TPayloadCreator1 extends (
        ...args: any[]
        ) => infer T
        ? T
        : undefined,
        TMeta1 extends unknown = TMetaCreator1 extends (...args: any[]) => infer T
        ? T
        : undefined,
        TPayload2 extends unknown = TPayloadCreator2 extends (
        ...args: any[]
        ) => infer T
        ? T
        : undefined,
        TMeta2 extends unknown = TMetaCreator2 extends (...args: any[]) => infer T
        ? T
        : undefined,
        TPayload3 extends unknown = TPayloadCreator3 extends (
        ...args: any[]
        ) => infer T
        ? T
        : undefined,
        TMeta3 extends unknown = TMetaCreator3 extends (...args: any[]) => infer T
        ? T
        : undefined,
        TPayload4 extends unknown = TPayloadCreator4 extends (
        ...args: any[]
        ) => infer T
        ? T
        : undefined,
        TMeta4 extends unknown = TMetaCreator4 extends (...args: any[]) => infer T
        ? T
        : undefined,
        TArgs1 extends any[] = TPayloadCreator1 extends (...args: infer T) => any
        ? T
        : TMetaCreator1 extends (...args: infer T) => any
        ? T
        : never,
        TArgs2 extends any[] = TPayloadCreator2 extends (...args: infer T) => any
        ? T
        : TMetaCreator2 extends (...args: infer T) => any
        ? T
        : never,
        TArgs3 extends any[] = TPayloadCreator3 extends (...args: infer T) => any
        ? T
        : TMetaCreator3 extends (...args: infer T) => any
        ? T
        : never,
        TArgs4 extends any[] = TPayloadCreator4 extends (...args: infer T) => any
        ? T
        : TMetaCreator4 extends (...args: infer T) => any
        ? T
        : never
        >(
        requestArg:
        | TType1
        | [TType1, TPayloadCreator1]
        | [TType1, TPayloadCreator1, TMetaCreator1],
        successArg:
        | TType2
        | [TType2, TPayloadCreator2]
        | [TType2, TPayloadCreator2, TMetaCreator2],
        failureArg:
        | TType3
        | [TType3, TPayloadCreator3]
        | [TType3, TPayloadCreator3, TMetaCreator3],
        cancelArg?:
        | TType4
        | [TType4, TPayloadCreator4]
        | [TType4, TPayloadCreator4, TMetaCreator4]
        ): AsyncAction<
        TType1,
        TPayload1,
        TMeta1,
        TArgs1,
        TType2,
        TPayload2,
        TMeta2,
        TArgs2,
        TType3,
        TPayload3,
        TMeta3,
        TArgs3,
        TType4,
        TPayload4,
        TMeta4,
        TArgs4
        >;
        };

          function createCustomAction

          createCustomAction: <
          TType extends string,
          TArgs extends any[] = [],
          TReturn extends unknown = {}
          >(
          type: TType,
          createHandler?: (...args: TArgs) => TReturn
          ) => ((...args: TArgs) => ResolveType<{ type: TType } & TReturn>) &
          ActionCreatorTypeMetadata<TType>;
          • create custom action-creator using constructor function with injected type argument

          function createReducer

          createReducer: <TState, TRootAction extends Action<string> = any>(
          initialState: TState,
          initialHandlers?: InitialHandler<TState, TRootAction>
          ) => Reducer<TState, TRootAction> &
          Readonly<{
          handlers: InitialHandler<TState, any>;
          handleAction: [unknown] extends [TRootAction]
          ? any
          : HandleActionChainApi<TState, TRootAction, TRootAction>;
          handleType: [unknown] extends [TRootAction]
          ? any
          : HandleTypeChainApi<TState, TRootAction, TRootAction>;
          }>;

            function getType

            getType: <TType extends string>(
            actionCreator: ActionCreator<TType> & ActionCreatorTypeMetadata<TType>
            ) => TType;
            • get the "type literal" of a given action-creator

            function isActionOf

            isActionOf: {
            <AC extends ActionCreator<{ type: string }>>(
            actionCreator: AC | AC[],
            action: { type: string }
            ): action is ReturnType<AC>;
            <AC extends ActionCreator<{ type: string }>>(
            actionCreator: AC | AC[]
            ): (action: { type: string }) => action is ReturnType<AC>;
            };
            • (curried assert function) check if an action is the instance of given action-creator(s) it works with discriminated union types

            function isOfType

            isOfType: {
            <T extends string, A extends { type: string }>(
            type: T | T[],
            action: A
            ): action is A extends { type: T } ? A : never;
            <T extends string>(type: T | T[]): <A extends { type: string }>(
            action: A
            ) => action is A extends { type: T } ? A : never;
            };
            • (curried assert function) check if action type is equal given type-constant it works with discriminated union types

            Interfaces

            interface ActionCreatorTypeMetadata

            interface ActionCreatorTypeMetadata<TType extends TypeConstant> {}
            • Type representing type getter on Action Creator instance

            property getType

            getType?: () => TType;

              interface Types

              interface Types {}
              • Interface for internal types augmentation

                Example 1

                ``` declare module 'typesafe-actions' { export type RootAction = ActionType<typeof import('./root-action').default>; export interface Types { RootAction: RootAction; } } ```

              Type Aliases

              type Action

              type Action<TType extends TypeConstant = TypeConstant> = {
              type: TType;
              };
              • Type representing Generic Action

              type ActionBuilder

              type ActionBuilder<
              TType extends TypeConstant,
              TPayload extends any = undefined,
              TMeta extends any = undefined
              > = [TMeta] extends [undefined]
              ? [TPayload] extends [undefined]
              ? unknown extends TPayload
              ? PayloadAction<TType, TPayload>
              : unknown extends TMeta
              ? PayloadMetaAction<TType, TPayload, TMeta>
              : EmptyAction<TType>
              : PayloadAction<TType, TPayload>
              : PayloadMetaAction<TType, TPayload, TMeta>;
              • todo

              type ActionCreator

              type ActionCreator<TType extends TypeConstant = TypeConstant> = (
              ...args: any[]
              ) => Action<TType>;
              • Type representing Generic ActionCreator

              type ActionCreatorBuilder

              type ActionCreatorBuilder<
              TType extends TypeConstant,
              TPayload extends any = undefined,
              TMeta extends any = undefined
              > = [TMeta] extends [undefined]
              ? [TPayload] extends [undefined]
              ? unknown extends TPayload
              ? PayloadActionCreator<TType, TPayload>
              : unknown extends TMeta
              ? PayloadMetaActionCreator<TType, TPayload, TMeta>
              : EmptyActionCreator<TType>
              : PayloadActionCreator<TType, TPayload>
              : PayloadMetaActionCreator<TType, TPayload, TMeta>;
              • todo

              type ActionType

              type ActionType<TActionCreatorOrMap extends any> =
              TActionCreatorOrMap extends ActionCreator<TypeConstant>
              ? ReturnType<TActionCreatorOrMap>
              : TActionCreatorOrMap extends Record<any, any>
              ? {
              [K in keyof TActionCreatorOrMap]: ActionType<TActionCreatorOrMap[K]>;
              }[keyof TActionCreatorOrMap]
              : TActionCreatorOrMap extends infer R
              ? never
              : never;
              • Infers Action union-type from action-creator map object

              type AsyncActionCreatorBuilder

              type AsyncActionCreatorBuilder<
              TRequest extends
              | [TType1, TPayload1]
              | [TType1, [TPayload1, TMeta1]]
              | [TType1, TArgs1, TPayload1]
              | [TType1, TArgs1, [TPayload1, TMeta1]],
              TSuccess extends
              | [TType2, TPayload2]
              | [TType2, [TPayload2, TMeta2]]
              | [TType2, TArgs2, TPayload2]
              | [TType2, TArgs2, [TPayload2, TMeta2]],
              TFailure extends
              | [TType3, TPayload3]
              | [TType3, [TPayload3, TMeta3]]
              | [TType3, TArgs3, TPayload3]
              | [TType3, TArgs3, [TPayload3, TMeta3]],
              TCancel extends
              | [TType4, TPayload4]
              | [TType4, [TPayload4, TMeta4]]
              | [TType4, TArgs4, TPayload4]
              | [TType4, TArgs4, [TPayload4, TMeta4]] = never,
              TType1 extends TypeConstant = TRequest[0],
              TPayload1 = TRequest extends [TType1, any, [any, any]]
              ? TRequest[2][0]
              : TRequest extends [TType1, any, any]
              ? TRequest[2]
              : TRequest extends [TType1, [any, any]]
              ? TRequest[1][0]
              : TRequest[1],
              TMeta1 = TRequest extends [TType1, any, [any, any]]
              ? TRequest[2][1]
              : TRequest extends [TType1, [any, any]]
              ? TRequest[1][1]
              : never,
              TArgs1 extends any[] = TRequest extends [TType1, any, any] ? TRequest[1] : never,
              TType2 extends TypeConstant = TSuccess[0],
              TPayload2 = TSuccess extends [TType2, any, [any, any]]
              ? TSuccess[2][0]
              : TSuccess extends [TType2, any, any]
              ? TSuccess[2]
              : TSuccess extends [TType2, [any, any]]
              ? TSuccess[1][0]
              : TSuccess[1],
              TMeta2 = TSuccess extends [TType2, any, [any, any]]
              ? TSuccess[2][1]
              : TSuccess extends [TType2, [any, any]]
              ? TSuccess[1][1]
              : never,
              TArgs2 extends any[] = TSuccess extends [TType2, any, any] ? TSuccess[1] : never,
              TType3 extends TypeConstant = TFailure[0],
              TPayload3 = TFailure extends [TType3, any, [any, any]]
              ? TFailure[2][0]
              : TFailure extends [TType3, any, any]
              ? TFailure[2]
              : TFailure extends [TType3, [any, any]]
              ? TFailure[1][0]
              : TFailure[1],
              TMeta3 = TFailure extends [TType3, any, [any, any]]
              ? TFailure[2][1]
              : TFailure extends [TType3, [any, any]]
              ? TFailure[1][1]
              : never,
              TArgs3 extends any[] = TFailure extends [TType3, any, any] ? TFailure[1] : never,
              TType4 extends TypeConstant = TCancel[0],
              TPayload4 = TCancel extends [TType4, any, [any, any]]
              ? TCancel[2][0]
              : TCancel extends [TType4, any, any]
              ? TCancel[2]
              : TCancel extends [TType4, [any, any]]
              ? TCancel[1][0]
              : TCancel[1],
              TMeta4 = TCancel extends [TType4, any, [any, any]]
              ? TCancel[2][1]
              : TCancel extends [TType4, [any, any]]
              ? TCancel[1][1]
              : never,
              TArgs4 extends any[] = TCancel extends [TType4, any, any] ? TCancel[1] : never
              > = [TCancel] extends [never]
              ? {
              request: [TArgs1] extends [never]
              ? ActionCreatorBuilder<TType1, TPayload1, TMeta1>
              : (...args: TArgs1) => ActionBuilder<TType1, TPayload1, TMeta1>;
              success: [TArgs2] extends [never]
              ? ActionCreatorBuilder<TType2, TPayload2, TMeta2>
              : (...args: TArgs2) => ActionBuilder<TType2, TPayload2, TMeta2>;
              failure: [TArgs3] extends [never]
              ? ActionCreatorBuilder<TType3, TPayload3, TMeta3>
              : (...args: TArgs3) => ActionBuilder<TType3, TPayload3, TMeta3>;
              }
              : {
              request: [TArgs1] extends [never]
              ? ActionCreatorBuilder<TType1, TPayload1, TMeta1>
              : (...args: TArgs1) => ActionBuilder<TType1, TPayload1, TMeta1>;
              success: [TArgs2] extends [never]
              ? ActionCreatorBuilder<TType2, TPayload2, TMeta2>
              : (...args: TArgs2) => ActionBuilder<TType2, TPayload2, TMeta2>;
              failure: [TArgs3] extends [never]
              ? ActionCreatorBuilder<TType3, TPayload3, TMeta3>
              : (...args: TArgs3) => ActionBuilder<TType3, TPayload3, TMeta3>;
              cancel: [TArgs4] extends [never]
              ? ActionCreatorBuilder<TType4, TPayload4, TMeta4>
              : (...args: TArgs4) => ActionBuilder<TType4, TPayload4, TMeta4>;
              };
              • todo

              type EmptyAction

              type EmptyAction<TType extends TypeConstant> = {
              type: TType;
              };
              • Action without Payload

              type EmptyActionCreator

              type EmptyActionCreator<TType extends TypeConstant> = () => EmptyAction<TType>;
              • Action Creator producing EmptyAction

              type PayloadAction

              type PayloadAction<TType extends TypeConstant, TPayload> = {
              type: TType;
              payload: TPayload;
              };
              • Action with only Payload

              type PayloadActionCreator

              type PayloadActionCreator<TType extends TypeConstant, TPayload> = (
              payload: TPayload
              ) => PayloadAction<TType, TPayload>;
              • Action Creator producing PayloadAction

              type PayloadMetaAction

              type PayloadMetaAction<TType extends TypeConstant, TPayload, TMeta> = {
              type: TType;
              payload: TPayload;
              meta: TMeta;
              };
              • Action with both Payload and Meta

              type PayloadMetaActionCreator

              type PayloadMetaActionCreator<TType extends TypeConstant, TPayload, TMeta> = (
              payload: TPayload,
              meta: TMeta
              ) => PayloadMetaAction<TType, TPayload, TMeta>;
              • Action Creator producing PayloadMetaAction

              type Reducer

              type Reducer<TState, TAction extends Action> = (
              state: TState | undefined,
              action: TAction
              ) => TState;
              • Type representing Generic Reducer

              type StateType

              type StateType<TReducerOrMap extends any> = TReducerOrMap extends Reducer<any, any>
              ? ReturnType<TReducerOrMap>
              : TReducerOrMap extends Record<any, any>
              ? {
              [K in keyof TReducerOrMap]: StateType<TReducerOrMap[K]>;
              }
              : never;
              • Infers State object from reducer map object

              type TypeConstant

              type TypeConstant = string;
              • Type representing Type Constant

              Package Files (11)

              Dependencies (0)

              No dependencies.

              Dev Dependencies (23)

              Peer Dependencies (0)

              No peer dependencies.

              Badge

              To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

              You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/typesafe-actions.

              • Markdown
                [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/typesafe-actions)
              • HTML
                <a href="https://www.jsdocs.io/package/typesafe-actions"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>