Use client-side callbacks like "onAdd" in typescript (solved)


is it possible, to use client-side callbacks like "onAdd" in typescript?

In the documentation is no example for typescript:

I tried the following code on my client-side:

import * as Colyseus from "colyseus.js"
var colyseusSDK = new Colyseus.Client("ws://localhost:2567");
colyseusSDK.joinOrCreate("my_room").then(function (room) {
    room.state.players.onAdd((player, sessionId) => {

but I get Property 'players' does not exist on type 'unknown'. as error in my IDE.

On the server side I have the following implementations:


import { Schema, MapSchema, type } from "@colyseus/schema";

export class Player extends Schema {
  @type("number") x: number;
  @type("number") y: number;
  @type("number") z: number;

export class MyRoomState extends Schema {
  @type({ map: Player }) players = new MapSchema<Player>();


import { Room, Client } from "colyseus";
import { MyRoomState, Player } from "./schema/MyRoomState";

export class MyRoom extends Room<MyRoomState> {

  onCreate (options: any) {
    this.setState(new MyRoomState());

    this.onMessage("type", (client, message) => {


  onJoin (client: Client, options: any) {
    console.log(client.sessionId, "joined!");

    const player = new Player();

    player.x = 1;
    player.y = 1;
    player.z = 1;

    this.state.players.set(client.sessionId, player);

  onLeave (client: Client, consented: boolean) {
    console.log(client.sessionId, "left!");

  onDispose() {
    console.log("room", this.roomId, "disposing...");

Hi there,
I think when you joined into a room and get state instance, it doesn't mean you've got all the data in state. (maybe changed in the future versions) so when you joined in a room, first hook onChange() to state of the room, whenever you got the full data in state, go on listen to it with onAdd() etc.