[Typescript] 86. Medium - ToPrimitive

Convert a property of type literal (label type) to a primitive type.

For example

type X = {
  name: 'Tom',
  age: 30,
  married: false,
  addr: {
    home: '123456',
    phone: '13111111111'
  }
}

type Expected = {
  name: string,
  age: number,
  married: boolean,
  addr: {
    home: string,
    phone: string
  }
}
type Todo = ToPrimitive<X> // should be same as `Expected`

 

/* _____________ Your Code Here _____________ */
type ArrayToPrimitive<T extends any[], ACC extends any[] = []> = T extends [infer F, ...infer RT] 
  ? [ValueToPrimitive<F>, ...ArrayToPrimitive<RT>] 
  : ACC;
type ObjectToPrimitive<T extends Record<PropertyKey, any>> = {
  [Key in keyof T]: T[Key] extends Record<PropertyKey, any> 
                      ? ObjectToPrimitive<T[Key]> 
                      : ValueToPrimitive<T[Key]>
}
type ValueToPrimitive<T extends any> = T extends string 
  ? string 
  : T extends number 
    ? number
    : T extends boolean 
      ? boolean
      : T extends undefined 
        ? undefined
        : T extends null 
          ? null
          : never;
type ToPrimitive<T extends Record<PropertyKey, any>> = {
  [Key in keyof T]: T[Key] extends any[] 
                      ? ArrayToPrimitive<T[Key]> 
                      : T[Key] extends object 
                        ? ObjectToPrimitive<T[Key]>
                        : ValueToPrimitive<T[Key]>
}

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type PersonInfo = {
  name: 'Tom'
  age: 30
  married: false
  addr: {
    home: '123456'
    phone: '13111111111'
  },
  hobbies: ['swim', 'sing']
}

type ExpectedResult = {
  name: string
  age: number
  married: boolean
  addr: {
    home: string
    phone: string
  }
  hobbies: [string, string]
}

type cases = [
  Expect<Equal<ToPrimitive<PersonInfo>, ExpectedResult>>,
]

 

posted @ 2022-11-02 14:31  Zhentiw  阅读(15)  评论(0编辑  收藏  举报