Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

JavaScript · 117 lines · 3.4 KiB · at 8b87e58

// The tsus runtime. Plain ESM, no classes, usable from Node and Cloudflare Workers.export const Some = (value) => ({  tag: "Some",  value,  isSome: () => true,  isNone: () => false,  map: (f) => Some(f(value)),  andThen: (f) => f(value),  unwrapOr: () => value,  okOr: () => Ok(value),});export const None = Object.freeze({  tag: "None",  isSome: () => false,  isNone: () => true,  map: () => None,  andThen: () => None,  unwrapOr: (fallback) => fallback,  okOr: (error) => Err(error),});export const Ok = (value) => ({  tag: "Ok",  value,  isOk: () => true,  isErr: () => false,  map: (f) => Ok(f(value)),  mapErr: () => Ok(value),  andThen: (f) => f(value),  unwrapOr: () => value,  ok: () => Some(value),});export const Err = (error) => ({  tag: "Err",  error,  isOk: () => false,  isErr: () => true,  map: () => Err(error),  mapErr: (f) => Err(f(error)),  andThen: () => Err(error),  unwrapOr: (fallback) => fallback,  ok: () => None,});export const tag = (t, value) => ({ tag: t, value });// --- compiled forms of the match macro ---//// The preprocessor lowers `match (subject) { ... }` to one of these, curried// so TypeScript fixes the subject's type before it checks the arms:////   __tsus_match(subject)({ Some: (x) => ..., None: () => ... })//   __tsus_match_or(subject)({ Some: (x) => ... }, () => wildcard)//   __tsus_match_lit(subject)([["/", () => ...], ...], () => wildcard)//// A variant arm receives the variant's payload: `value` if it has one (Some,// Ok, Tagged), else `error` (Err), else the variant object itself.const payload = (v) => ("value" in v ? v.value : "error" in v ? v.error : v);const noArm = (value) =>  new Error(`TSUS100: non-exhaustive match, no arm matched ${JSON.stringify(value)}. Add a _ arm.`);const dispatch = (value, arms, otherwise) => {  const t = value !== null && typeof value === "object" ? value.tag : undefined;  if (typeof t === "string" && Object.hasOwn(arms, t)) {    return arms[t](payload(value));  }  if (otherwise) {    return otherwise(value);  }  throw noArm(value);};export const __tsus_match = (value) => (arms) => dispatch(value, arms);export const __tsus_match_or = (value) => (arms, otherwise) => dispatch(value, arms, otherwise);export const __tsus_match_lit = (value) => (cases, otherwise) => {  for (const [literal, handler] of cases) {    if (value === literal) {      return handler();    }  }  if (otherwise) {    return otherwise(value);  }  throw noArm(value);};// --- compiled form of the ? operator ---////   const x = load()?;// becomes//   const t = __tsus_try(load()); if (__tsus_isBail(t)) return __tsus_bail(t); const x = t.value;//// __tsus_try and __tsus_bail are identities at runtime; they exist for the// type checker. An Err or None carries no payload of the success type, so the// same object is returned as-is from the enclosing function.export const __tsus_try = (value) => value;export const __tsus_isBail = (value) => value.tag === "Err" || value.tag === "None";export const __tsus_bail = (value) => value;// Emitted only for a ? the compiler already rejected (TSUS300/TSUS303), so a// checked build never contains it; reaching it means the checker was bypassed.export const __tsus_unwrap = (value) => {  if (value.tag === "Ok" || value.tag === "Some") {    return value.value;  }  throw new Error(`TSUS300: ? propagated ${value.tag} outside a Result/Option function`);};