typescript TS -如何对泛型类型中的类型值执行字符串函数

jpfvwuh4  于 2022-12-05  发布在  TypeScript
关注(0)|答案(1)|浏览(136)

我有一个valuesToUpperCase泛型,我想把一个类型作为输入,把它的值大写并返回
我尝试过此方法,但得到错误消息'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(),
  }
o4tp2gmn

o4tp2gmn1#

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

相关问题