Write pick(obj, keys): return a new object with only the listed keys, typed to match.
The function works, but its types don't help: any key compiles, and the result is a Record<string, unknown>. Type it so the keys must be keys of obj, and the result has exactly those properties with their types. Skip keys that the object doesn't have, so an optional property stays missing.
function pick(obj: Record<string, unknown>, keys: string[]): Record<string, unknown> { const result: Record<string, unknown> = {}; for (const key of keys) result[key] = obj[key]; return result;} // const user = { id: 1, name: "Ada", password: "secret" };// pick(user, ["id", "name"]); // { id: 1, name: "Ada" }, typed { id: number; name: string }// pick(user, ["email"]); // should be a type errorThe answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…