在 Typescript 中使用泛型类型加宽类型

Ben*_*arp 8 typescript

在某些情况下,我想扩大按字面转换的对象的类型(使用“as const”),因此它的属性将被推断为字符串或数字,而不是字面意义。

想象一下我有以下类型

const obj = [
   {
      type:"student", 
      name:"Yossi"
   }, 
   {
      type: "Teacher", 
      name: "Lili"
   }
] as const

type Person = typeof obj [number]
Run Code Online (Sandbox Code Playgroud)

我希望从字面上推断 obj 的类型,但要更广泛地推断 Person,因此它的类型和名称是字符串。是否有一个泛型可以允许以下内容:

type Person = Widen<typeof obj [number]>
Run Code Online (Sandbox Code Playgroud)

Mac*_*ora 8

有趣的案例。我们可以尝试通过映射类型来创建这样的实用程序。考虑:

// it transforms our specific types into primitive origins
type ToPrimitive<T> =
  T extends string ? string
  : T extends number ? number
  : T extends boolean ? boolean
  : T;
// mapped types which will preserve keys with more wide value types
type Widen<O> = {
  [K in keyof O]: ToPrimitive<O[K]>
}
// using
type Person = Widen<typeof obj[number]>
const a: Person = {
  name: 'name', // string
  type: 'type' // string
}

Run Code Online (Sandbox Code Playgroud)

我们可以ToPrimitive通过添加附加条件来扩展以考虑其他类型,如对象、数组。

正如我所看到的,您obj的元素类型或原始类型是 one - {name: string, type: string}。然后我们可以通过以下方式从第一个元素创建一个类型:

type Person = Widen<typeof obj[0]>;
// and this nicely evaluates to:
type Person = {
    readonly type: string;
    readonly name: string;
}
Run Code Online (Sandbox Code Playgroud)