- Notifications
You must be signed in to change notification settings - Fork27
Type-safe action creator utilities
License
aikoven/typescript-fsa
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
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;}
npm install --save redux-typescript-actionsimportactionCreatorFactoryfrom'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 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}
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'}}
// 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;};
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.
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
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors13
Uh oh!
There was an error while loading.Please reload this page.