Skip to content

Instantly share code, notes, and snippets.

@Xevion
Created April 17, 2025 22:42
Show Gist options
  • Save Xevion/1d13f31a4c1efd1e23a8d724817960e3 to your computer and use it in GitHub Desktop.
Save Xevion/1d13f31a4c1efd1e23a8d724817960e3 to your computer and use it in GitHub Desktop.
import { fromJson as fromResultJson } from "@/lib/true-myth/result";
import { fromJson as fromMaybeJson } from "@/lib/true-myth/maybe";
export function unreachable(value: never): never {
throw new Error(`Should have been unreachable with ${value}`);
}
export { fromResultJson, fromMaybeJson };
import { unreachable } from "@/lib/true-myth";
import Maybe, * as maybe from "true-myth/maybe";
export function fromJson<T extends object>(json: maybe.MaybeJSON<T>): Maybe<T> {
switch (json.variant) {
case "Just":
return Maybe.just(json.value);
case "Nothing":
return Maybe.nothing();
default:
unreachable(json);
}
}
import { unreachable } from "@/lib/true-myth";
import Result, * as result from "true-myth/result";
export function fromJson<T, E>(json: result.ResultJSON<T, E>): Result<T, E> {
switch (json.variant) {
case "Ok":
return Result.ok(json.value);
case "Err":
return Result.err(json.error);
default:
unreachable(json);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment