Skip to content

Commit 01615af

Browse files
committed
improve toJSON() return type.
1 parent cf02f84 commit 01615af

File tree

4 files changed

+17
-33
lines changed

4 files changed

+17
-33
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@colyseus/schema",
3-
"version": "2.0.12",
3+
"version": "2.0.13",
44
"description": "Binary state serializer with delta encoding for games",
55
"bin": {
66
"schema-codegen": "./bin/schema-codegen"

src/Schema.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { CollectionSchema } from './types/CollectionSchema';
1111
import { SetSchema } from './types/SetSchema';
1212

1313
import { ChangeTree, Ref, ChangeOperation } from "./changes/ChangeTree";
14-
import { NonFunctionPropNames } from './types/HelperTypes';
14+
import { NonFunctionPropNames, ToJSON } from './types/HelperTypes';
1515
import { ClientState } from './filters';
1616
import { getType } from './types/typeRegistry';
1717
import { ReferenceTracker } from './changes/ReferenceTracker';
@@ -184,7 +184,7 @@ export abstract class Schema {
184184
}
185185

186186
public assign(
187-
props: { [prop in NonFunctionPropNames<this>]?: this[prop] }
187+
props: { [prop in NonFunctionPropNames<this>]?: this[prop] } | ToJSON<this>,
188188
) {
189189
Object.assign(this, props);
190190
return this;
@@ -898,7 +898,7 @@ export abstract class Schema {
898898
const schema = this._definition.schema;
899899
const deprecated = this._definition.deprecated;
900900

901-
const obj = {}
901+
const obj: ToJSON<typeof this> = {};
902902
for (let field in schema) {
903903
if (!deprecated[field] && this[field] !== null && typeof (this[field]) !== "undefined") {
904904
obj[field] = (typeof (this[field]['toJSON']) === "function")

src/types/HelperTypes.ts

+12-28
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
1-
type Bool = 'true' | 'false'
2-
type Key = string | number | symbol;
1+
import { ArraySchema } from "./ArraySchema";
2+
import { MapSchema } from "./MapSchema";
33

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]>;
307

318
export type NonFunctionPropNames<T> = {
329
[K in keyof T]: T[K] extends Function ? never : K
3310
}[keyof T];
3411

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+
}>>;

test/SchemaTest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ describe("Schema Usage", () => {
973973

974974
state.value.set('k1', 1);
975975
const firstEncoded = state.encodeAll();
976-
976+
977977
const decodedState1 = new TestMapSchema();
978978
decodedState1.decode(firstEncoded);
979979
assert.deepStrictEqual(decodedState1.toJSON(), state.toJSON(), `decodedState1.toJSON() EQ state.toJSON()`);

0 commit comments

Comments
 (0)