审查这段生成的事件解析与摘要代码。
解析不可信的任务事件,只接受字面量标签与载荷都有效的数据,并在不做无用工作的前提下穷尽处理各状态,返回包含任务 id 和状态专属信息的描述。
TypeScript
type JobEvent =
| { kind: "started"; jobId: string }
| { kind: "completed"; jobId: string; resultUrl: string }
| { kind: "failed"; jobId: string; message: string };
function parseEvent(raw: string): JobEvent {
const event = JSON.parse(raw) as JobEvent;
return event;
}
function summarize(event: JobEvent): string {
const copy = { ...event };
switch (copy.kind) {
case "completed":
return copy.resultUrl;
case "failed":
return copy.message;
default:
return "started";
}
}
生成代码仅作示例,不代表任何特定模型