Skip to content

Commit

Permalink
TypeScript: fixes .toJSON() return type.
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Oct 9, 2023
1 parent 5cb5b06 commit c662ecc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@colyseus/schema",
"version": "2.0.16",
"version": "2.0.17",
"description": "Binary state serializer with delta encoding for games",
"bin": {
"schema-codegen": "./bin/schema-codegen"
Expand Down Expand Up @@ -78,7 +78,7 @@
"source-map-support": "^0.5.13",
"ts-node": "^7.0.1",
"tslib": "^2.1.0",
"tsx": "^3.12.7",
"tsx": "^3.13.0",
"typescript": "^5.0.4"
},
"nyc": {
Expand Down
4 changes: 2 additions & 2 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,15 +898,15 @@ export abstract class Schema {
const schema = this._definition.schema;
const deprecated = this._definition.deprecated;

const obj: ToJSON<typeof this> = {};
const obj: unknown = {};
for (let field in schema) {
if (!deprecated[field] && this[field] !== null && typeof (this[field]) !== "undefined") {
obj[field] = (typeof (this[field]['toJSON']) === "function")
? this[field]['toJSON']()
: this[`_${field}`];
}
}
return obj;
return obj as ToJSON<typeof this>;
}

discardAllChanges() {
Expand Down
4 changes: 2 additions & 2 deletions src/types/HelperTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export type NonFunctionPropNames<T> = {
[K in keyof T]: T[K] extends Function ? never : K
}[keyof T];

export type ToJSON<T> = Partial<NonFunctionProps<{
export type ToJSON<T> = NonFunctionProps<{
[K in keyof T]: T[K] extends MapSchema<infer U>
? Record<string, U>
: T[K] extends ArraySchema<infer U>
? U[]
: T[K]
}>>;
}>;

0 comments on commit c662ecc

Please sign in to comment.