Home > Enterprise >  TS - How to execute a string function on a type value inside a type generic
TS - How to execute a string function on a type value inside a type generic

Time:12-01

I have this valuesToUpperCase generic, where I want to get a type as an input and uppercase its values and return it

I have tried this but I get an error saying 'toUppercase' implicitly has an 'any' return type

type GeneralType = {
  name: string;
  car: string;
}

  type valuesToUpperCase<T extends GeneralType> = {
    name: T['firstName'].toUpperCase(),
    car: T['lastName'].toUpperCase(),
  }

CodePudding user response:

type GeneralType = {
  name: 'asIgbiB';
  car: 'iaygfIUGI';
}

type x = Uppercase
//        ^? type Uppercase<S extends string> = intrisic

type AllAsUppercase<T extends Record<string, string>> = {
   [K in keyof T]: Uppercase<T[K]>
}

type y = AllAsUppercase<GeneralType>
//   ^?

Playground

  • Related