0.1.0 • Published 4 years ago

safetypings v0.1.0

Weekly downloads
7
License
MIT
Repository
-
Last release
4 years ago

safetypings

Typescript utilities for easier typing in redux (observable).

Aggregated from typesafe-actions.

Usage

ActionType

Useful utility type that will infer action types from action creator map, e.g.

// types.d.ts
import { ActionType } from 'safetypings';
import * as actions from './actions';

export type RootAction = ActionType<typeof actions>;

It only extracts types that are action creators.

isOfType

Check if passed action is equal given action type, e.g.

import { RootAction, RootState, Services } from 'MyTypes';
import { Epic } from 'redux-observable';
import { filter } from 'rxjs/operators';
import { isOfType } from 'safetypings';
import * as actions from './actions';

export const loginEpic: Epic<RootAction, RootAction, RootState, Services> = (
  action$,
  state$,
  { ajax },
) =>
  action$.pipe(
    filter(isOfType(actions.LOGIN)),
    // value type is now narrowed to `{ type: 'LOGIN', payload: { username: string } }`
  );

isPresent

Check if value passed is present (non-nullable), e.g.

action$.pipe(
  // ...
  map(getAccessToken),
  filter(isPresent),
  // removes null and undefined from value type
);