panic.flow

1.0.0 • Public • Published

panic.flow

travis package downloads styled with prettier

Utility for enforcing type encoded invariants at runtime for untyped call site. More simply a way to throw an exception from code paths that aren't possible accordnig to a type checker but still possible by incorrect call from untyped JS.

Usage

import panic from "panic.flow"

type Message = "increment" | "decrement"

export const update = (state: number, message: Message): number => {
  switch (message) {
    case "increment":
      return state + 1
    case "decrement":
      return state - 1
    default:
      return panic(`Unabel to handle invalid message: ${message}`)
  }
}

Note that above code type checks fine, but following call site on the other hand does not:

update(5, "sqrt")
//        ^ Cannot call `update` with `"sqrt"` bound to `message` because
// string [1] is incompatible with enum [2].
// References:
// update(5, "sqrt")
//           ^ [1]
// export const update = (state:number, message:Message):number => {
//                                              ^ [2]

But call site may not use type checker, in that case panic could be utilized to produce runtime error.

If panic is invoked with a string argument Error instance with a given message is thrown:

try {
  panic("Boom!")
} catch (error) {
  console.log(error) // Error("Boom!")
}

If panic is invoked without any argument Error with default message is thrown:

try {
  panic()
} catch (error) {
  console.log(error)
  // Error("Unreachable code (according to type-checker) was exectude, likely due to invalid usage.")
}

If panic is invoked with any other argument it's will be thrown as is:

try {
  panic(NaN)
} catch (error) {
  console.log(error) // NaN
}

Install

npm install panic.flow

Package Sidebar

Install

npm i panic.flow

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

10.4 kB

Total Files

17

Last publish

Collaborators

  • gozala