Code Monkey home page Code Monkey logo

unionize's Introduction

Unionize Build Status Coverage Status

Define unions via records for great good!

Installation

yarn add unionize

Example

Call unionize or unionizeCustom on a record literal mapping tag literals to value types:

import { unionize, ofType } from 'unionize'

// Define a record mapping tag literals to value types
const Action = unionize({
  ADD_TODO:                ofType<{ id: string; text: string }>(),
  SET_VISIBILITY_FILTER:   ofType<'SHOW_ALL' | 'SHOW_ACTIVE' | 'SHOW_COMPLETED'>(),
  TOGGLE_TODO:             ofType<{ id: string }>(),
}, 'type', 'payload'); // optionally override tag and value property names

Extract the inferred tagged union:

type Action = typeof Action._Union;

The inferred type is

type Action =
  | { type: ADD_TODO; payload: { id: string; text: string } }
  | { type: SET_VISIBILITY_FILTER; payload: 'SHOW_ALL' | 'SHOW_ACTIVE' | 'SHOW_COMPLETED' }
  | { type: TOGGLE_TODO; payload: { id: string } }

Having done that, you now have at your disposal:

Element factories

store.dispatch(Action.ADD_TODO({ id: 'c819bbc1', text: 'Take out the trash' }));
store.dispatch(Action.SET_VISIBILITY_FILTER('SHOW_COMPLETED'));

Match expressions

const todosReducer = (state: Todo[] = [], action: Action) => Action.match({
  // handle cases as pure functions instead of switch statements
  ADD_TODO: ({ id, text }) => [
    ...state,
    { id, text, completed: false }
  ],
  TOGGLE_TODO: ({ id }) => state.map(todo =>
    todo.id === id
      ? {...todo, completed: !todo.completed}
      : todo
  )
}, () => state /* default; if not provided, cases must be exhaustive */)(action);

Type predicates

(action$: Observable<Action>) => action$
  .filter(Action.is.TOGGLE_TODO)
  // The type of the resulting observable is refined appropriately...
  .mergeMap(({ text }) => /*...*/)

Type casts

const { id, text } = Action.as.ADD_TODO(someAction) // throws if someAction is not an ADD_TODO

unionize's People

Contributors

pelotom avatar sledorze avatar

Watchers

Aitor Oses avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.