审查生成的账户更新边界

来自 工具类型
TypeScript 6 高级 12分钟 找出 4处问题

在这段生成的更新、投影与处理器代码用于账户请求前,对它进行审查。

只允许更新 displayName 与嵌套 timezone,保留现有 theme,从运行时响应中删除 passwordHash,并报告未知处理器名称。

TypeScript
interface Account {
  id: string;
  displayName: string;
  role: "user" | "admin";
  passwordHash: string;
  preferences: { theme: "light" | "dark"; timezone: string };
}
type AccountPatch = Partial<Account>;
function applyPatch(current: Account, patch: AccountPatch): Account {
  return { ...current, ...patch };
}

function publicAccount(account: Account): Omit<Account, "passwordHash"> {
  return account;
}

type Handler = (account: Account) => string;
const handlers: Record<string, Handler> = {
  summary: (account) => account.displayName,
};

function runHandler(name: string, account: Account): string {
  return handlers[name](account);
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误