首页 > 其他分享 >[Typescript] 86. Medium - ToPrimitive

[Typescript] 86. Medium - ToPrimitive

时间:2022-11-02 14:44:56浏览次数:64  
标签:Typescript string boolean 86 married Medium extends Key type

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>>,
]

 

标签:Typescript,string,boolean,86,married,Medium,extends,Key,type
From: https://www.cnblogs.com/Answer1215/p/16850947.html

相关文章

  • Typescript 编译模块
    Typescript有一种统一的语言处理模块的导入和导出,在编译时可以根据最终js文件应用到哪一种模块系统,根据模块系统的规范,生成代码原始Ts文件importm=require("mod");......
  • TypeScript 常用类型
    typescript常用类型js已有类型原始类型:number\string\boolean\null\underfined\symbol对象类型:object(包括数组、对象、函数等对象)ts新增类型......
  • [Typescript] 84. Medium - GetMiddleElement
    Getthemiddleelementofthearraybyimplementinga GetMiddleElement method,representedbyanarrayIfthelengthofthearrayisodd,returnthemiddle......
  • [Typescript] 83. Medium - Subsequence
    Givenanarrayofuniqueelements,returnallpossiblesubsequences.Asubsequenceisasequencethatcanbederivedfromanarraybydeletingsomeornoeleme......
  • 【XSY4186】Binomial(结论,数位DP)
    题面Binomial题解设\(\operatorname{ord}(n)\)表示\(n\)分解质因数后\(p\)的幂次,那么我们就是对于每一个\(k\)要求有多少\(0\leqm\leqn\)使得\(\operatorn......
  • typescript number 转 string(转)
    转自:number转string一、双点解析10..toString();二、括号先计算再转换(10).toString();三、加空串10+''转自:number转string......
  • [Typescript] 81. Medium - Number Range
    Sometimeswewanttolimittherangeofnumbers...Forexamples.typeresult=NumberRange<2,9>//|2|3|4|5|6|7|8|9 /*_____________Your......
  • [Typescript] 80. Medium - Construct Tuple
    Constructatuplewithagivenlength.Forexampletyperesult=ConstructTuple<2>//expecttobe[unknown,unkonwn] /*_____________YourCodeHere______......
  • typescript文件导入svelte文件报错处理办法
    在typescripts文件中引入svelte文件时报错Cannotfindmodule'../components/HelloWorld.svelte'oritscorrespondingtypedeclarations.ts(2307)并且svelte已安装......
  • 【XSY1976】【BZOJ2286】【SDOI2011】消耗战(虚树,dp)
    这题的dp思想还是比较容易想的,关键是如何保证时间复杂度,这时就用到了虚树的技巧。1.虚树是什么?(虚树的性质)不妨设现在询问给出了\(k\)个点,我们命名这些节点为关键节点。......