0

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 strings, right? So when I do values[field] TypeScript should know that the resulting value must be a string!

What am I missing here?

Stefano Verna
  • 642
  • 9
  • 23
  • This is a known [design limitation](https://github.com/Microsoft/TypeScript/issues/30728#issuecomment-479542352) in TypeScript. I'm almost certain I've answered this question before on SO; let me see if I can dredge up a link – jcalz Nov 18 '19 at 00:45
  • btw [this](https://www.typescriptlang.org/play/#code/GYVwdgxgLglg9mABABwE52QU1VAngNQEMAbETAFTgFVktUJCBnTAHgGlFMAPKTMAE0aIA1plxxgiQmFwAaROU48+gxACVMEOKn7t5jKKhhgA5gD4zACgBuJMowBcC+cBiZi-J2wCUiAN4AUIjBiKiYUCCoSLakmIwA2q7u-AC6AHRQ1LTYAMJMmJbeANwBAL4BAWgY2HhEsZQ0dAzMln5STgBEhB3yAEZOAIzyEE6GZIil8l0dxUA) is easier for the compiler to understand – jcalz Nov 18 '19 at 00:48

0 Answers0