Skip to main content
Module

x/eitherway/mod.ts>unsafeCastTo

Yet Another Option and Result Implementation - providing safe abstractions for fallible flows inspired by F# and Rust
Latest
function unsafeCastTo
import { unsafeCastTo } from "https://deno.land/x/eitherway@0.10.0/mod.ts";

Use this to cast a unknown value to a known type.

This is mostly useful when integrating external, fallible code, where one ought to deal with thrown exceptions (and this type is in fact documented).

Please note, that this is unsafe and only provided for prototyping purposes and experimentation.

Examples

Lifting a fallible function

import { Result } from "https://deno.land/x/eitherway/mod.ts";
import { unsafeCastTo } from "./mod.ts";

function parseHex(hex: string): number {
  const candidate = Number.parseInt(hex, 16);

  if (Number.isNaN(candidate)) throw TypeError();
  return candidate;
}

const safeParseHex = Result.liftFallible(
  parseHex,
  unsafeCastTo<TypeError>,
);

const res = Result("0x10f2c").andThen(safeParseHex);

const value: number = res.unwrapOr(0);

Parameters

err: unknown