MapTypes
介绍
实现MapTypes<T,R>
, 转换对象T中的类型为由R结构定义的不同类型
ts
type StringToNumber = { mapFrom: string; mapTo: number;}MapTypes<{iWillBeANumberOneDay: string}, StringToNumber> // gives { iWillBeANumberOneDay: number; }
ts
type StringToNumber = { mapFrom: string; mapTo: number;}MapTypes<{iWillBeANumberOneDay: string}, StringToNumber> // gives { iWillBeANumberOneDay: number; }
Be aware that user can provide a union of types:
ts
type StringToNumber = { mapFrom: string; mapTo: number;}type StringToDate = { mapFrom: string; mapTo: Date;}MapTypes<{iWillBeNumberOrDate: string}, StringToDate | StringToNumber> // gives { iWillBeNumberOrDate: number | Date; }
ts
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:
ts
type StringToNumber = { mapFrom: string; mapTo: number;}MapTypes<{iWillBeANumberOneDay: string, iWillStayTheSame: Function}, StringToNumber> // // gives { iWillBeANumberOneDay: number, iWillStayTheSame: Function }
View on GitHubts
type StringToNumber = { mapFrom: string; mapTo: number;}MapTypes<{iWillBeANumberOneDay: string, iWillStayTheSame: Function}, StringToNumber> // // gives { iWillBeANumberOneDay: number, iWillStayTheSame: Function }
起点
ts
/* _____________ Your Code Here _____________ */typeMapTypes <T ,R > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{stringToArray : string }, {mapFrom : string,mapTo : [] }>, {stringToArray : [] }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{stringToNumber : string }, {mapFrom : string,mapTo : number }>, {stringToNumber : number }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{stringToNumber : string,skipParsingMe : boolean }, {mapFrom : string,mapTo : number }>, {stringToNumber : number,skipParsingMe : boolean }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{date : string }, {mapFrom : string,mapTo :Date } | {mapFrom : string,mapTo : null }>, {date : null |Date }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{date : string }, {mapFrom : string,mapTo :Date | null }>, {date : null |Date }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{fields :Record <string, boolean> }, {mapFrom :Record <string, boolean>,mapTo : string[] }>, {fields : string[] }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{name : string }, {mapFrom : boolean,mapTo : never }>, {name : string }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{name : string,date :Date }, {mapFrom : string,mapTo : boolean } | {mapFrom :Date ,mapTo : string }>, {name : boolean,date : string }>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeMapTypes <T ,R > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{stringToArray : string }, {mapFrom : string,mapTo : [] }>, {stringToArray : [] }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{stringToNumber : string }, {mapFrom : string,mapTo : number }>, {stringToNumber : number }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{stringToNumber : string,skipParsingMe : boolean }, {mapFrom : string,mapTo : number }>, {stringToNumber : number,skipParsingMe : boolean }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{date : string }, {mapFrom : string,mapTo :Date } | {mapFrom : string,mapTo : null }>, {date : null |Date }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{date : string }, {mapFrom : string,mapTo :Date | null }>, {date : null |Date }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{fields :Record <string, boolean> }, {mapFrom :Record <string, boolean>,mapTo : string[] }>, {fields : string[] }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{name : string }, {mapFrom : boolean,mapTo : never }>, {name : string }>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MapTypes <{name : string,date :Date }, {mapFrom : string,mapTo : boolean } | {mapFrom :Date ,mapTo : string }>, {name : boolean,date : string }>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
typeMapTypes <T ,R > = any
ts
typeMapTypes <T ,R > = any