@@ -23,11 +23,12 @@ export class ObjectSchema<T extends SchemaProperties, O extends InferedPropertie
23
23
24
24
read ( input : ISerialInput ) : O {
25
25
// Sorting the keys in ASCII ascending order, so that the order is platform independent.
26
- const keys : string [ ] = Object . keys ( this . properties ) . sort ( ) ;
27
- let result : any = { } ;
26
+ const keys : ( keyof T ) [ ] = Object . keys ( this . properties ) . sort ( ) ;
27
+ const result = { } as O ;
28
28
29
29
for ( const key of keys ) {
30
- result [ key ] = this . properties [ key ] . read ( input ) ;
30
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
31
+ result [ key ] = this . properties [ key ] . read ( input ) as O [ typeof key ] ;
31
32
}
32
33
33
34
return result ;
@@ -45,16 +46,16 @@ export class ObjectSchema<T extends SchemaProperties, O extends InferedPropertie
45
46
}
46
47
}
47
48
48
- type InferedSubTypes < T extends { [ key in keyof T ] : ObjectSchema < any > } > = {
49
+ type InferedSubTypes < T extends { [ key in keyof T ] : ObjectSchema < SchemaProperties > } > = {
49
50
[ Key in keyof T ] : T [ Key ] [ '_infered' ] & { type : Key }
50
51
} ;
51
52
52
53
export type ObjectSchemaMap < S , SI extends { [ key in keyof SI ] : SI [ key ] } > = { [ key in keyof S ] : ObjectSchema < SI [ key ] > } ;
53
54
54
55
export class GenericObjectSchema <
55
56
T extends SchemaProperties , // Base properties
56
- S extends { [ key in keyof S ] : ObjectSchema < any > } , // Sub type map
57
- K extends ( keyof S extends string ? SubTypeKey . STRING : SubTypeKey . ENUM )
57
+ S extends { [ Key in keyof S ] : ObjectSchema < SchemaProperties > } , // Sub type map
58
+ K extends ( ( keyof S ) extends string ? SubTypeKey . STRING : SubTypeKey . ENUM )
58
59
> extends ObjectSchema < T , InferedProperties < T > & InferedSubTypes < S > [ keyof S ] > {
59
60
constructor (
60
61
public readonly keyedBy : K ,
@@ -72,7 +73,7 @@ export class GenericObjectSchema<
72
73
// Figuring out sub-types
73
74
const subTypeDescription = this . getSubTypeMap ( ) [ value . type ] || null ;
74
75
if ( subTypeDescription === null ) {
75
- throw new Error ( `Unknown sub-type '${ value . type } ' in among '${ JSON . stringify ( Object . keys ( this . subTypeMap ) ) } '` ) ;
76
+ throw new Error ( `Unknown sub-type '${ value . type . toString ( ) } ' in among '${ JSON . stringify ( Object . keys ( this . subTypeMap ) ) } '` ) ;
76
77
}
77
78
78
79
// Writing the sub-type out.
@@ -87,10 +88,12 @@ export class GenericObjectSchema<
87
88
super . write ( output , value ) ;
88
89
89
90
// Extra sub-type fields
91
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
90
92
const extraKeys : string [ ] = Object . keys ( subTypeDescription . properties ) . sort ( ) ;
91
93
92
94
for ( const key of extraKeys ) {
93
- const prop = subTypeDescription . properties [ key ] ;
95
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
96
+ const prop : Schema < unknown > = subTypeDescription . properties [ key ] ;
94
97
95
98
prop . write ( output , value [ key ] ) ;
96
99
}
@@ -105,18 +108,18 @@ export class GenericObjectSchema<
105
108
throw new Error ( `Unknown sub-type '${ subTypeKey } ' in among '${ JSON . stringify ( Object . keys ( subTypeMap ) ) } '` ) ;
106
109
}
107
110
108
- let result : any = super . read ( input ) ;
111
+ const result = super . read ( input ) ;
109
112
110
113
// Making the sub type key available to the result object.
111
- result . type = subTypeKey ;
114
+ result . type = subTypeKey as keyof S ;
112
115
113
116
if ( subTypeDescription !== null ) {
114
- const extraKeys : string [ ] = Object . keys ( subTypeDescription . properties ) . sort ( ) ;
117
+ const extraKeys = Object . keys ( subTypeDescription . properties ) . sort ( ) ;
115
118
116
119
for ( const key of extraKeys ) {
117
120
const prop = ( subTypeDescription . properties ) [ key ] ;
118
-
119
- result [ key ] = prop . read ( input ) ;
121
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
122
+ ( result as any ) [ key ] = prop . read ( input ) ;
120
123
}
121
124
}
122
125
@@ -132,12 +135,12 @@ export class GenericObjectSchema<
132
135
// Extra sub-type fields
133
136
const subTypeDescription = this . getSubTypeMap ( ) [ value . type ] || null ;
134
137
if ( subTypeDescription === null ) {
135
- throw new Error ( `Unknown sub-type '${ value . type } ' in among '${ JSON . stringify ( Object . keys ( this . subTypeMap ) ) } '` ) ;
138
+ throw new Error ( `Unknown sub-type '${ value . type . toString ( ) } ' in among '${ JSON . stringify ( Object . keys ( this . subTypeMap ) ) } '` ) ;
136
139
}
137
140
138
141
size += Object . keys ( subTypeDescription . properties ) // Going through extra property keys
139
142
. map ( key => subTypeDescription . properties [ key ] . sizeOf ( value [ key ] ) ) // Mapping extra properties into their sizes
140
- . reduce ( ( a , b ) => a + b ) ; // Summing them up
143
+ . reduce ( ( a , b ) => a + b , 0 ) ; // Summing them up
141
144
142
145
return size ;
143
146
}
0 commit comments