Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

TypeScript · 31 lines · 1.0 KiB · at 9472e3c

// Plausible bug: truthiness checks and `??` defaults instead of explicit// presence checks; blank host treated as missing; numeric strings coerced.export type Config = {  host: string;  port: number;  debug: boolean;  timeoutMs?: number;  tags: string[];};export type ConfigErrorKind = "missing" | "wrong_type" | "out_of_range";export class ConfigError extends Error {  constructor(    public readonly kind: ConfigErrorKind,    public readonly field: string,  ) {    super(`${kind}: ${field}`);  }}export function parseConfig(raw: Record<string, unknown>): Config {  if (!raw.host) throw new ConfigError("missing", "host");  if (typeof raw.host !== "string") throw new ConfigError("wrong_type", "host");  const port = Number(raw.port ?? 8080);  if (port < 1 || port > 65535) throw new ConfigError("out_of_range", "port");  const debug = Boolean(raw.debug);  const timeoutMs = raw.timeoutMs ? (raw.timeoutMs as number) : undefined;  const tags = (raw.tags as string[]) || [];  return { host: raw.host.trim(), port, debug, timeoutMs, tags };}