@freckle/exhaustive
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@freckle/exhaustive-js

Provides a helper function for checking exhaustiveness. Exhaustiveness checking is a feature of a language where the type checker guarantees that all cases were covered.

Usage

import {exhaustive} from '@freckle/exhaustive-js'

type Status = 'success' | 'failure'

function getStatusMessage(status: Status): string {
  switch (status) {
    case 'success':
      return 'The operation completed successfully.'
    case 'failure':
      return 'The operation failed.'
    default:
      return exhaustive(status)
  }
}

Usage in a reducer where returning the state itself in the default case is a common pattern:

import {exhaustiveReducer} from '@freckle/exhaustive-js'

type TodoState = {...}
type TodoAction = {type: 'todoAdded', ...} | {type: 'todoToggled', ...}

const defaultTodoState = {...}

function todoReducer(
  state: TodoState = defaultTodoState,
  action: TodoAction
): TodoState {
  switch (action.type) {
    case 'todoAdded':
      return {...}
    case 'todoToggled':
      return {...}
    default:
      return exhaustiveReducer(action.type, state)
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @freckle/exhaustive

Weekly Downloads

691

Version

1.0.1

License

MIT

Unpacked Size

6.83 kB

Total Files

13

Last publish

Collaborators

  • luckysoni
  • z0isch
  • stackptr
  • carlos-cubas
  • freckle-engineering