typescript-fsa

  • Version 3.0.0
  • Published
  • 33.1 kB
  • No dependencies
  • MIT license

Install

npm i typescript-fsa
yarn add typescript-fsa
pnpm add typescript-fsa

Overview

Type-safe action creator utilities

Index

Functions

function actionCreatorFactory

actionCreatorFactory: (
prefix?: string | null,
defaultIsError?: (payload: any) => boolean
) => ActionCreatorFactory;
  • Creates Action Creator factory with optional prefix for action types.

    Parameter prefix

    Prefix to be prepended to action types as <prefix>/<type>.

    Parameter defaultIsError

    Function that detects whether action is error given the payload. Default is payload => payload instanceof Error.

function isType

isType: <Payload>(
action: AnyAction,
actionCreator: ActionCreator<Payload>
) => action is Action<Payload>;
  • Returns true if action has the same type as action creator. Defines Type Guard that lets TypeScript know payload type inside blocks where isType returned true.

    Example 1

    const somethingHappened = actionCreator<{foo: string}>('SOMETHING_HAPPENED');

    if (isType(action, somethingHappened)) { // action.payload has type {foo: string} }

Interfaces

interface Action

interface Action<Payload> extends AnyAction {}

    property error

    error?: boolean;

      property meta

      meta?: Meta;

        property payload

        payload: Payload;

          property type

          type: string;

            interface ActionCreator

            interface ActionCreator<Payload> {}

              property match

              match: (action: AnyAction) => action is Action<Payload>;
              • Identical to isType except it is exposed as a bound method of an action creator. Since it is bound and takes a single argument it is ideal for passing to a filtering function like Array.prototype.filter or RxJS's Observable.prototype.filter.

                Example 1

                const somethingHappened = actionCreator<{foo: string}>('SOMETHING_HAPPENED'); const somethingElseHappened = actionCreator<{bar: number}>('SOMETHING_ELSE_HAPPENED');

                if (somethingHappened.match(action)) { // action.payload has type {foo: string} }

                const actionArray = [ somethingHappened({foo: 'foo'}), somethingElseHappened({bar: 5}), ];

                // somethingHappenedArray has inferred type Action<{foo: string}>[] const somethingHappenedArray = actionArray.filter(somethingHappened.match);

              property type

              type: string;

                call signature

                (payload: Payload, meta?: Meta): Action<Payload>;
                • Creates action with given payload and metadata.

                  Parameter payload

                  Action payload.

                  Parameter meta

                  Action metadata. Merged with commonMeta of Action Creator.

                interface ActionCreatorFactory

                interface ActionCreatorFactory {}

                  method async

                  async: <Params, Result, Error = {}>(
                  type: string,
                  commonMeta?: Meta
                  ) => AsyncActionCreators<Params, Result, Error>;
                  • Creates three Action Creators: * started: ActionCreator<Params> * done: ActionCreator<{params: Params, result: Result}> * failed: ActionCreator<{params: Params, error: Error}>

                    Useful to wrap asynchronous processes.

                    Parameter type

                    Prefix for types of created actions, which will have types ${type}_STARTED, ${type}_DONE and ${type}_FAILED.

                    Parameter commonMeta

                    Metadata added to created actions.

                  call signature

                  <Payload = void>(
                  type: string,
                  commonMeta?: Meta,
                  isError?: boolean
                  ): ActionCreator<Payload>;
                  • Creates Action Creator that produces actions with given type and payload of type Payload.

                    Parameter type

                    Type of created actions.

                    Parameter commonMeta

                    Metadata added to created actions.

                    Parameter isError

                    Defines whether created actions are error actions.

                  call signature

                  <Payload = void>(
                  type: string,
                  commonMeta?: Meta,
                  isError?: (payload: Payload) => boolean
                  ): ActionCreator<Payload>;
                  • Creates Action Creator that produces actions with given type and payload of type Payload.

                    Parameter type

                    Type of created actions.

                    Parameter commonMeta

                    Metadata added to created actions.

                    Parameter isError

                    Function that detects whether action is error given the payload.

                  interface AnyAction

                  interface AnyAction {}

                    property type

                    type: any;

                      interface AsyncActionCreators

                      interface AsyncActionCreators<Params, Result, Error = {}> {}

                        property done

                        done: ActionCreator<Success<Params, Result>>;

                          property failed

                          failed: ActionCreator<Failure<Params, Error>>;

                            property started

                            started: ActionCreator<Params>;

                              property type

                              type: string;

                                Type Aliases

                                type Failure

                                type Failure<Params, Error> = (
                                | {
                                params: Params;
                                }
                                | (Params extends void
                                ? {
                                params?: Params;
                                }
                                : never)
                                ) & {
                                error: Error;
                                };

                                  type Meta

                                  type Meta = null | {
                                  [key: string]: any;
                                  };

                                    type Success

                                    type Success<Params, Result> = (
                                    | {
                                    params: Params;
                                    }
                                    | (Params extends void
                                    ? {
                                    params?: Params;
                                    }
                                    : never)
                                    ) &
                                    (
                                    | {
                                    result: Result;
                                    }
                                    | (Result extends void
                                    ? {
                                    result?: Result;
                                    }
                                    : never)
                                    );

                                      Package Files (1)

                                      Dependencies (0)

                                      No dependencies.

                                      Dev Dependencies (12)

                                      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/typescript-fsa.

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