I'm having hard times trying to figure it out why the following does not work:
type StringPropertyNames<T> =
{
[K in keyof T]: T[K] extends string ? K : never
}[keyof T]
function propertyValueToUppercase<T>(values: T, field: StringPropertyNames<T>) {
// Property 'toUpperCase' does not exist on type 'T[{ [K in keyof T]: T[K] extends string ? K : never; }[keyof T]]'
return values[field].toUpperCase();
}
StringPropertyNames<T>
should be a union of all the properties of the generic type that store string
s, right? So when I do values[field]
TypeScript should know that the resulting value must be a string!
What am I missing here?