Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Text · 24 lines · 996 B · at 83c2f03

// A boundary file (*.boundary.tsus): the one place where outside data is// trusted after a runtime check. Casts (TSUS402) and default exports// (TSUS406) are allowed here; every other rule still applies.import { Ok, Err, type Result } from "@tsus/runtime";import { wrapSync } from "@tsus/runtime/boundary";export type Config = { readonly port: number; readonly host: string };export type ConfigError = "unparseable" | "invalid shape";const parseJson = wrapSync(JSON.parse);const looksLikeConfig = (value: unknown): boolean => {  const fields = Object(value) as { port?: unknown; host?: unknown };  return typeof fields.port === "number" && typeof fields.host === "string";};const fail = (error: ConfigError): Result<Config, ConfigError> => Err(error);export const loadConfig = (raw: string): Result<Config, ConfigError> =>  match (parseJson(raw)) {    Ok(data) => (looksLikeConfig(data) ? Ok(data as Config) : fail("invalid shape")),    Err(e) => fail("unparseable"),  };