I like the tricks :) – Kasia
You can prevent accessing undefined properties at compile time
const pick = (keys: string[], object: Record<string, any>): Record<string, any> =>
keys.reduce((acc, key) => ({ ...acc, [key]: object[key] }), {})
pick(['name', 'age'], { name: 'riccardo', age: 18, color: 'pink' })
// { "name": "riccardo", "age": 18 }
pick(['hah!'], { name: 'riccardo', age: 18, color: 'pink' })
// ⛔️ { "hah!": undefined }
const pick = <R extends Record<string, unknown>>
(keys: (keyof R)[], object: R): Record<string, unknown> =>
keys.reduce((acc, key) => ({ ...acc, [key]: object[key] }), {})
pick(['name', 'age'], { name: 'riccardo', age: 18, color: 'pink' })
// { "name": "riccardo", "age": 18 }
pick(['hah!'], { name: 'riccardo', age: 18, color: 'pink' })
// ✅ Does not compile
Crafted with 💛 by Riccardo
Crafted with 💛 by Riccardo