Skip to content

Commit

Permalink
TypeScript: fixes ToJSON return type. Regression from 01615af
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Nov 16, 2023
1 parent 3c510f7 commit 372a898
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@colyseus/schema",
"version": "2.0.23",
"version": "2.0.24",
"description": "Binary state serializer with delta encoding for games",
"bin": {
"schema-codegen": "./bin/schema-codegen"
Expand Down
8 changes: 5 additions & 3 deletions src/types/HelperTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export type NonFunctionPropNames<T> = {
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]
: T[K] extends Map<string, infer U>
? Record<string, U>
: T[K] extends ArraySchema<infer U>
? U[]
: T[K]
}>;
26 changes: 25 additions & 1 deletion test/TypeScriptTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ describe("TypeScript Types", () => {
orderPriority: null,
}));
state.encodeAll();
console.log("DONE!");
assert.ok(true);
});

describe("complex declaration scenarios", () => {
it("implements / extends without conflicts", () => {
// Defines a generic schema
interface SchemaInterface extends Schema {
players: Map<string, string>;
items: string[];
}

// Implements the above interface
// MapSchema is compatible with Map
class SchemaInterfaceImpl extends Schema implements SchemaInterface {
players: MapSchema<string>;
items: ArraySchema<string>;
}

// Uses the schema interface
abstract class AbstractRoom<T extends SchemaInterface> { }

// Uses the schema implementation
class AbstractRoomImpl extends AbstractRoom<SchemaInterfaceImpl> { }
});

})
});

0 comments on commit 372a898

Please sign in to comment.