File tree Expand file tree Collapse file tree 4 files changed +48
-15
lines changed Expand file tree Collapse file tree 4 files changed +48
-15
lines changed Original file line number Diff line number Diff line change @@ -7,21 +7,27 @@ type MethodsAndProperties<T> = { [key in keyof T]: T[key] };
77
88type Properties < T > = Omit < MethodsAndProperties < T > , Methods < T > > ;
99
10- type PrimitiveTypes = string | number | boolean | Date | undefined | null ;
10+ type PrimitiveTypes = string | number | boolean | undefined | null ;
11+
12+ type DateToNumber < T > = T extends Date ? number : T ;
1113
1214type ValueObjectValue < T > = T extends PrimitiveTypes
1315 ? T
14- : T extends { value : infer U }
15- ? U
16- : T extends Array < { value : infer U } >
17- ? U [ ]
18- : T extends Array < infer U >
19- ? Array < ValueObjectValue < U > >
20- : T extends { [ K in keyof Properties < T > ] : unknown }
21- ? { [ K in keyof Properties < T > ] : ValueObjectValue < Properties < T > [ K ] > }
22- : T extends unknown
23- ? T
24- : never ;
16+ : T extends Date
17+ ? number
18+ : T extends { value : infer U }
19+ ? DateToNumber < U >
20+ : T extends Array < { value : infer U } >
21+ ? DateToNumber < U > [ ]
22+ : T extends Array < infer U >
23+ ? Array < ValueObjectValue < U > >
24+ : T extends { [ K in keyof Properties < T > ] : unknown }
25+ ? {
26+ [ K in keyof Properties < T > ] : ValueObjectValue < Properties < T > [ K ] > ;
27+ }
28+ : T extends unknown
29+ ? DateToNumber < T >
30+ : never ;
2531
2632export type Primitives < T > = {
2733 [ key in keyof Properties < T > ] : ValueObjectValue < T [ key ] > ;
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { DeliveryInfo } from "./DeliveryInfo";
66import { Learner } from "./Learner" ;
77import { Metadata } from "./Metadata" ;
88import { Product } from "./Product" ;
9+ import { Step } from "./Step" ;
910import { User } from "./User" ;
1011import { Video } from "./Video" ;
1112
@@ -63,7 +64,18 @@ describe("Primitives", () => {
6364
6465 type expectedPrimitives = {
6566 readonly active : boolean ;
66- readonly createdAt : Date ;
67+ readonly name : string ;
68+ } ;
69+
70+ expectTypeOf < actualPrimitives > ( ) . toEqualTypeOf < expectedPrimitives > ( ) ;
71+ } ) ;
72+
73+ it ( "should get primitive number type from Date" , ( ) => {
74+ type actualPrimitives = Primitives < Step > ;
75+
76+ type expectedPrimitives = {
77+ readonly name : string ;
78+ readonly publishedAt : number ;
6779 } ;
6880
6981 expectTypeOf < actualPrimitives > ( ) . toEqualTypeOf < expectedPrimitives > ( ) ;
Original file line number Diff line number Diff line change @@ -3,13 +3,13 @@ import { Primitives } from "../src";
33export class Product {
44 constructor (
55 public readonly active : boolean ,
6- public readonly createdAt : Date ,
6+ public readonly name : string ,
77 ) { }
88
99 toPrimitives ( ) : Primitives < Product > {
1010 return {
1111 active : this . active ,
12- createdAt : this . createdAt ,
12+ name : this . name ,
1313 } ;
1414 }
1515}
Original file line number Diff line number Diff line change 1+ import { Primitives } from "../src" ;
2+
3+ export class Step {
4+ constructor (
5+ public readonly name : string ,
6+ public readonly publishedAt : Date ,
7+ ) { }
8+
9+ toPrimitives ( ) : Primitives < Step > {
10+ return {
11+ name : this . name ,
12+ publishedAt : this . publishedAt . getTime ( ) ,
13+ } ;
14+ }
15+ }
You can’t perform that action at this time.
0 commit comments