首页 > 其他分享 >[Typescript] 79. Medium - MapTypes

[Typescript] 79. Medium - MapTypes

时间:2022-10-29 16:45:18浏览次数:55  
标签:mapFrom Typescript string number MapTypes Medium Expect type 79

Implement MapTypes<T, R> which will transform types in object T to different types defined by type R which has the following structure

type StringToNumber = {
  mapFrom: string; // value of key which value is string
  mapTo: number; // will be transformed for number
}

Examples:

type StringToNumber = { mapFrom: string; mapTo: number;}
MapTypes<{iWillBeANumberOneDay: string}, StringToNumber> // gives { iWillBeANumberOneDay: number; }

Be aware that user can provide a union of types:

type StringToNumber = { mapFrom: string; mapTo: number;}
type StringToDate = { mapFrom: string; mapTo: Date;}
MapTypes<{iWillBeNumberOrDate: string}, StringToDate | StringToNumber> // gives { iWillBeNumberOrDate: number | Date; }

If the type doesn't exist in our map, leave it as it was:

type StringToNumber = { mapFrom: string; mapTo: number;}
MapTypes<{iWillBeANumberOneDay: string, iWillStayTheSame: Function}, StringToNumber> // // gives { iWillBeANumberOneDay: number, iWillStayTheSame: Function }

 

/* _____________ Your Code Here _____________ */

type MapTypes<T extends Record<PropertyKey, any>, R extends Record<'mapFrom' | 'mapTo', any>> = {
  [K in keyof T]: T[K] extends R['mapFrom']
    ? R extends {mapFrom: T[K]}                        // solve the union case, to skip double mapping from
      ? R['mapTo']
      : never
    : T[K]
}
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<MapTypes<{ stringToArray: string }, { mapFrom: string; mapTo: [] }>, { stringToArray: [] }>>,
  Expect<Equal<MapTypes<{ stringToNumber: string }, { mapFrom: string; mapTo: number }>, { stringToNumber: number }>>,
  Expect<Equal<MapTypes<{ stringToNumber: string; skipParsingMe: boolean }, { mapFrom: string; mapTo: number }>, { stringToNumber: number; skipParsingMe: boolean }>>,
  Expect<Equal<MapTypes<{ date: string }, { mapFrom: string; mapTo: Date } | { mapFrom: string; mapTo: null }>, { date: null | Date }>>,
  Expect<Equal<MapTypes<{ date: string }, { mapFrom: string; mapTo: Date | null }>, { date: null | Date }>>,
  Expect<Equal<MapTypes<{ fields: Record<string, boolean> }, { mapFrom: Record<string, boolean>; mapTo: string[] }>, { fields: string[] }>>,
  Expect<Equal<MapTypes<{ name: string }, { mapFrom: boolean; mapTo: never }>, { name: string }>>,
  Expect<Equal<MapTypes<{ name: string; date: Date }, { mapFrom: string; mapTo: boolean } | { mapFrom: Date; mapTo: string }>, { name: boolean; date: string }>>,
]

 

标签:mapFrom,Typescript,string,number,MapTypes,Medium,Expect,type,79
From: https://www.cnblogs.com/Answer1215/p/16839021.html

相关文章

  • [Typescript] 78. Medium - Unqiue
    ImplementthetypeversionofLodash.uniq,UniquetakesanArrayT,returnstheArrayTwithoutrepeatedvalues.typeRes=Unique<[1,1,2,2,3,3]>;//exp......
  • 【TypeScript】语法精讲
    TypeScript的编译环境#安装命令npminstalltypescript-g#查看版本tsc--versionTypeScript的运行环境#安装ts-nodenpminstallts-node-g#另外ts-node需要依赖tslib......
  • typeScript基础语法
    1.变量定义变量名:变量的类型(首字母小写)letdan:string='蛋'letnumArr:number[]=[4,2,0];2、核心定义任何东西的时候要注明类型调用任何东西的时候要检查类型3......
  • react实战笔记79:加载meals数据2
     通过父子传值处理......
  • 【P8179】【EZEC-11】Tyres(背包问题,决策单调性,分治)
    和这道题的题面很像,但是做法不同。题面:有\(n\)家商店,第\(i\)家商店一共可以卖出\(m_i\)件商品,其中第\(j\)件商品购买所需的代价为\(a_{i,j}\)。特别地,对于第\(......
  • [Typescript] 75. Easy - Push
    Implementthegenericversionof Array.pushForexample:typeResult=Push<[1,2],'3'>//[1,2,'3'] /*_____________YourCodeHere_____________*/t......
  • [Typescript] 77. Easy - Unshift
    Implementthetypeversionof Array.unshiftForexample:typeResult=Unshift<[1,2],0>//[0,1,2,] /*_____________YourCodeHere_____________*/t......
  • [Typescript] 76. Easy - Parameters
    Implementthebuilt-inParametersgenericwithoutusingit.Forexample:constfoo=(arg1:string,arg2:number):void=>{}typeFunctionParamsType=MyPara......
  • 1879. 两个数组最小的异或值之和
    题目描述给了两个数组nums1和nums2,长度都是n,问怎么排列可以让数组对应元素的异或值之和最小?f1-二进制枚举+状态压缩基本分析1.有没有是啥贪心做法,因为看到相同元素异或......
  • uva 11795
      题目意思还是有点绕的,想了好一会大体上是一个状压dp(废话 f[j],当当前拥有武器集合为j时,消灭所有怪物的方案数像线性dp一样,但可以去掉一维 f[j]+=f[t],t=j^(1......