[Typescript] 79. Medium - MapTypes
typescript Medium 79
2023-09-14 09:00:44 时间
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 }>>,
]
相关文章
- [Typescript] 83. Medium - Subsequence
- [Typescript] 81. Medium - Number Range
- [Typescript] 80. Medium - Construct Tuple
- [Typescript] 72. Medium - IndexOf
- [Typescript] 57. Medium - FlattenDepth
- [Typescript] 51. Medium - Mutable
- [Typescript] 44. Medium - Drop char
- [Typescript Challenges] 9. Easy - Concat
- [Typescript v4.1] Template type literals
- [Tools] Unlock TypeScript's Features in Vanilla JS with @ts-check and JSDoc
- [Typescript] 69. Medium - Trim Right
- [Typescript] 39. Medium - AnyOf
- [Typescript] 36. Medium - Merge
- [Typescript] 22. Medium - Pop
- 什么是 TypeScript 的 Module Augmentation
- 什么是 TypeScript 变量的 declared type
- TypeScript里的类型为any和泛型的区别