I have a function typed as follows:
const getByPrimaryKey = <E>(list: E[], primaryKey: keyof E): E => {...}
Can I add a type validation to the value associated to primaryKey ? For exemple:
interface User {
id: string
name?: string
age: number
}
const users: User[] = [{id: '1', age: 1}]
getByPrimaryKey<User>(users, 'id') // OK since id is a string
getByPrimaryKey<User>(users, 'name') // NOT OK because name maybe undefined
getByPrimaryKey<User>(users, 'age') // NOT OK because age is a number