Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Type-safe action creator utilities

License

NotificationsYou must be signed in to change notification settings

aikoven/typescript-fsa

Repository files navigation

A simple Action Creator library for TypeScript. Its goal is to provide simpleyet type-safe experience with Redux actions.Created actions are FSA-compliant:

interfaceAction<P>{type:string;payload:P;error?:boolean;meta?:Object;}

Installation

npm install --save redux-typescript-actions

Usage

Basic

importactionCreatorFactoryfrom'redux-typescript-actions';constactionCreator=actionCreatorFactory();// Specify payload shape as generic type argument.constsomethingHappened=actionCreator<{foo:string}>('SOMETHING_HAPPENED');// Get action creator type.console.log(somethingHappened.type);// SOMETHING_HAPPENED// Create action.constaction=somethingHappened({foo:'bar'});console.log(action);// {type: 'SOMETHING_HAPPENED', payload: {foo: 'bar'}}

Async Action Creators

Async Action Creators are objects with propertiesstarted,done andfailed whose values are action creators.

importactionCreatorFactoryfrom'redux-typescript-actions';constactionCreator=actionCreatorFactory();// specify parameters and result shapes as generic type argumentsconstdoSomething=actionCreator.async<{foo:string},// parameter type{bar:number},// success type{code:number}// error type>('DO_SOMETHING');console.log(doSomething.started({foo:'lol'}));// {type: 'DO_SOMETHING_STARTED', payload: {foo: 'lol'}}console.log(doSomething.done({params:{foo:'lol'},result:{bar:42},});// {type: 'DO_SOMETHING_DONE', payload: {//   params: {foo: 'lol'},//   result: {bar: 42},// }}console.log(doSomething.failed({params:{foo:'lol'},error:{code:42},});// {type: 'DO_SOMETHING_FAILED', payload: {//   params: {foo: 'lol'},//   error: {code: 42},// }, error: true}

Actions With Type Prefix

You can specify a prefix that will be prepended to all action types. This isuseful to namespace library actions as well as for large projects where it'sconvenient to keep actions near the component that dispatches them.

// MyComponent.actions.tsimportactionCreatorFactoryfrom'redux-typescript-actions';constactionCreator=actionCreatorFactory('MyComponent');constsomethingHappened=actionCreator<{foo:string}>('SOMETHING_HAPPENED');constaction=somethingHappened({foo:'bar'});console.log(action);// {type: 'MyComponent/SOMETHING_HAPPENED', payload: {foo: 'bar'}}

Reducers

// actions.tsimportactionCreatorFactoryfrom'redux-typescript-actions';constactionCreator=actionCreatorFactory();exportconstsomethingHappened=actionCreator<{foo:string}>('SOMETHING_HAPPENED');// reducer.tsimport{ActionasReduxAction}from'redux';import{isType,Action}from'redux-typescript-actions';import{somethingHappened}from'./actions';typeState={bar:string};constreducer=(state:State,action:ReduxAction):State=>{if(isType(action,somethingHappened)){// action.payload is inferred as {foo: string};action.payload.bar;// errorreturn{bar:action.payload.foo};}returnstate;};

API

actionCreatorFactory(prefix?: string, defaultIsError?: Predicate): ActionCreatorFactory

Creates Action Creator factory with optional prefix for action types.

  • prefix?: string: Prefix to be prepended to action types.
  • defaultIsError?: Predicate: Function that detects whether action is errorgiven the payload. Default ispayload => payload instanceof Error.

isType(action: Action, actionCreator: ActionCreator): boolean

Returnstrue if action has the same type as action creator. DefinesType Guardthat lets TypeScript knowpayload type inside blocks whereisType returnedtrue:

constsomethingHappened=actionCreator<{foo:string}>('SOMETHING_HAPPENED');if(isType(action,somethingHappened)){// action.payload has type {foo: string};}

About

Type-safe action creator utilities

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors13


[8]ページ先頭

©2009-2025 Movatter.jp