Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Text · 26 lines · 701 B · at f30ac63

import { z } from "zod";import { Ok, Err, type Result } from "@tsus/runtime";const UserSchema = z.object({  name: z.string(),  age: z.number().int().min(0),});type User = z.infer<typeof UserSchema>;const parseUser = (input: unknown): Result<User, string> => {  const outcome = UserSchema.safeParse(input);  return outcome.success ? Ok(outcome.data) : Err(outcome.error.issues[0].message);};const good = parseUser({ name: "justin", age: 30 });const bad = parseUser({ name: "justin", age: "old" });console.log(match (good) {  Ok(u) => `${u.name} is ${u.age}`,  Err(e) => `rejected: ${e}`,});console.log(match (bad) {  Ok(u) => `${u.name} is ${u.age}`,  Err(e) => "rejected",});