When using a predicate function with pick, be aware of potential type inaccuracies due to TypeScript's partial type matching.
If the object passed at runtime contains properties that are not defined in its TypeScript interface, the key and value parameters in your callback will still reflect the types defined in the interface. This means the TypeScript compiler might believe certain branches of logic (like an else block for unexpected keys) are unreachable, even though they may execute at runtime.
// Example demonstrating potential inaccuracy in `key` and `value` types within `_.pick` callback
import * as _ from 'radashi'
// @noErrors
interface User {
name: string
age: number
}
function getUserDetails(user: User) {
return _.pick(user, (value, key) => {
// TypeScript believes `key` is 'name' | 'age', but at runtime
// it could be 'email'
if (key === 'name' || key === 'age') {
console.log(key, '=', value)
} else {
// TypeScript believes this will never run, but it does.
console.log('Unexpected key:', key)
}
})
}
// At runtime, the function may receive an object with more properties
const runtimeUser = {
name: 'John',
age: 30,
// This property is not listed in the User type:
email: 'john@example.com',
}
getUserDetails(runtimeUser)
// Logs the following:
// name = John
// age = 30
// Unexpected key: email