Review this generated job-update decoder before it receives production events.
Decode untrusted JSON without leaking the token, return invalid for malformed data, preserve a legitimate 0% update, and make future union members fail exhaustiveness checks.
TypeScript
type JobUpdate =
| { kind: "progress"; percent: number; token: string }
| { kind: "failed"; message: string; token: string }
| { kind: "done"; artifact: string; token: string };
function decodeUpdate(raw: string): JobUpdate {
const value = JSON.parse(raw) as JobUpdate;
return value;
}
function summarize(update: JobUpdate): string {
console.log("update", JSON.stringify(update));
if (update.kind === "progress") {
if (!update.percent) return "waiting";
return `${update.percent}%`;
}
if (update.kind === "failed") return update.message;
return "finished";
}
generated code is illustrative, not from any one model