import { unsafeCastTo } from "https://deno.land/x/eitherway@0.10.0/lib/core/errors.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
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);