File tree 4 files changed +48
-15
lines changed
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] };
7
7
8
8
type Properties < T > = Omit < MethodsAndProperties < T > , Methods < T > > ;
9
9
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 ;
11
13
12
14
type ValueObjectValue < T > = T extends PrimitiveTypes
13
15
? 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 ;
25
31
26
32
export type Primitives < T > = {
27
33
[ 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";
6
6
import { Learner } from "./Learner" ;
7
7
import { Metadata } from "./Metadata" ;
8
8
import { Product } from "./Product" ;
9
+ import { Step } from "./Step" ;
9
10
import { User } from "./User" ;
10
11
import { Video } from "./Video" ;
11
12
@@ -63,7 +64,18 @@ describe("Primitives", () => {
63
64
64
65
type expectedPrimitives = {
65
66
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 ;
67
79
} ;
68
80
69
81
expectTypeOf < actualPrimitives > ( ) . toEqualTypeOf < expectedPrimitives > ( ) ;
Original file line number Diff line number Diff line change @@ -3,13 +3,13 @@ import { Primitives } from "../src";
3
3
export class Product {
4
4
constructor (
5
5
public readonly active : boolean ,
6
- public readonly createdAt : Date ,
6
+ public readonly name : string ,
7
7
) { }
8
8
9
9
toPrimitives ( ) : Primitives < Product > {
10
10
return {
11
11
active : this . active ,
12
- createdAt : this . createdAt ,
12
+ name : this . name ,
13
13
} ;
14
14
}
15
15
}
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