I have a setup that looks like this:
interface ValueAccessor<T> {
property: keyof T;
getPropertyValue: (value: any) => value;
}
I am trying to figure out how to define the correct type and replace the any
when I want to provide a custom getPropertyValue
function to retrieve a specific property of T
.
The desired interface I'm looking for is:
interface User {
username: string;
email: string;
}
const valueAccessor: ValueAccessor<User> = {
property: 'username',
getPropertyValueFunction: …
// The goal here is for the type checker to identify that
// the proper type of this function should be: (value: string) => string, as
// the property selected is 'username' and the type of User['username'] is string.
}
If I wish to access another property like email
:
const valueAccessor: ValueAccessor<User> = {
property: 'email',
getPropertyValueFunction: … Expected type: (value: string) => string
}