Commit 01615af 1 parent cf02f84 commit 01615af Copy full SHA for 01615af
File tree 4 files changed +17
-33
lines changed
4 files changed +17
-33
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " @colyseus/schema" ,
3
- "version" : " 2.0.12 " ,
3
+ "version" : " 2.0.13 " ,
4
4
"description" : " Binary state serializer with delta encoding for games" ,
5
5
"bin" : {
6
6
"schema-codegen" : " ./bin/schema-codegen"
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import { CollectionSchema } from './types/CollectionSchema';
11
11
import { SetSchema } from './types/SetSchema' ;
12
12
13
13
import { ChangeTree , Ref , ChangeOperation } from "./changes/ChangeTree" ;
14
- import { NonFunctionPropNames } from './types/HelperTypes' ;
14
+ import { NonFunctionPropNames , ToJSON } from './types/HelperTypes' ;
15
15
import { ClientState } from './filters' ;
16
16
import { getType } from './types/typeRegistry' ;
17
17
import { ReferenceTracker } from './changes/ReferenceTracker' ;
@@ -184,7 +184,7 @@ export abstract class Schema {
184
184
}
185
185
186
186
public assign (
187
- props : { [ prop in NonFunctionPropNames < this> ] ?: this[ prop ] }
187
+ props : { [ prop in NonFunctionPropNames < this> ] ?: this[ prop ] } | ToJSON < this > ,
188
188
) {
189
189
Object . assign ( this , props ) ;
190
190
return this ;
@@ -898,7 +898,7 @@ export abstract class Schema {
898
898
const schema = this . _definition . schema ;
899
899
const deprecated = this . _definition . deprecated ;
900
900
901
- const obj = { }
901
+ const obj : ToJSON < typeof this > = { } ;
902
902
for ( let field in schema ) {
903
903
if ( ! deprecated [ field ] && this [ field ] !== null && typeof ( this [ field ] ) !== "undefined" ) {
904
904
obj [ field ] = ( typeof ( this [ field ] [ 'toJSON' ] ) === "function" )
Original file line number Diff line number Diff line change 1
- type Bool = 'true' | 'false'
2
- type Key = string | number | symbol ;
1
+ import { ArraySchema } from "./ArraySchema" ;
2
+ import { MapSchema } from "./MapSchema" ;
3
3
4
- type Not < X extends Bool > = {
5
- true : 'false' ,
6
- false : 'true'
7
- } [ X ]
8
-
9
- type HaveIntersection < S1 extends string , S2 extends string > = (
10
- { [ K in S1 ] : 'true' } &
11
- { [ key : string ] : 'false' }
12
- ) [ S2 ]
13
-
14
- type IsNeverWorker < S extends Key > = (
15
- { [ K in S ] : 'false' } &
16
- { [ key : string ] : 'true' }
17
- ) [ S ]
18
-
19
- // Worker needed because of https://github.com/Microsoft/TypeScript/issues/18118
20
- type IsNever < T extends Key > = Not < HaveIntersection < IsNeverWorker < T > , 'false' > >
21
-
22
- type IsFunction < T > = IsNever < keyof T >
23
-
24
- export type NonFunctionProps < T > = {
25
- [ K in keyof T ] : {
26
- 'false' : K ,
27
- 'true' : never
28
- } [ IsFunction < T [ K ] > ]
29
- } [ keyof T ] ;
4
+ export type NonFunctionProps < T > = Omit < T , {
5
+ [ K in keyof T ] : T [ K ] extends Function ? K : never ;
6
+ } [ keyof T ] > ;
30
7
31
8
export type NonFunctionPropNames < T > = {
32
9
[ K in keyof T ] : T [ K ] extends Function ? never : K
33
10
} [ keyof T ] ;
34
11
12
+ export type ToJSON < T > = Partial < NonFunctionProps < {
13
+ [ K in keyof T ] : T [ K ] extends MapSchema < infer U >
14
+ ? Record < string , U >
15
+ : T [ K ] extends ArraySchema < infer U >
16
+ ? U [ ]
17
+ : T [ K ]
18
+ } > > ;
Original file line number Diff line number Diff line change @@ -973,7 +973,7 @@ describe("Schema Usage", () => {
973
973
974
974
state . value . set ( 'k1' , 1 ) ;
975
975
const firstEncoded = state . encodeAll ( ) ;
976
-
976
+
977
977
const decodedState1 = new TestMapSchema ( ) ;
978
978
decodedState1 . decode ( firstEncoded ) ;
979
979
assert . deepStrictEqual ( decodedState1 . toJSON ( ) , state . toJSON ( ) , `decodedState1.toJSON() EQ state.toJSON()` ) ;
You can’t perform that action at this time.
0 commit comments