Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Text · 30 lines · 807 B · at 355e324

import { Ok, Err, tag, type Result, type Tagged } from "@tsus/runtime";type PortError = Tagged<"NotANumber", string> | Tagged<"OutOfRange", number>;const parsePort = (raw: string): Result<number, PortError> => {  const n = Number(raw);  if (Number.isNaN(n)) {    return Err(tag("NotANumber", raw));  }  if (n < 1 || n > 65535) {    return Err(tag("OutOfRange", n));  }  return Ok(n);};const explain = (e: PortError): string =>  match (e) {    NotANumber(raw) => `not a number: ${raw}`,    OutOfRange(n) => `out of range: ${n}`,  };const report = (raw: string): string =>  match (parsePort(raw)) {    Ok(port) => `listening on ${port}`,    Err(reason) => `bad port (${explain(reason)})`,  };console.log(report("8080"));console.log(report("banana"));console.log(report("99999"));