Hi
I have project Phaser 3 + TypeScript+ Socket.io
In main client file index.ts I have this
export class Game extends Phaser.Game {
public socket: any;
public CANVAS_WIDTH: number;
public CANVAS_HEIGHT: number;
constructor(socket) {
super(mainGameConfig);
this.socket = socket;
this.CANVAS_WIDTH = this.config.width as number;
this.CANVAS_HEIGHT = this.config.height as number;
}
}
new Game(socket);```
When I use `this.game.socket.***` in different other files (for example in class Phaser.Scene) I have an error in IDE `Property 'socket' does not exist on type 'Game'.ts(2339)` and error in console, when I build app `[tsl] ERROR in /home/aliaksei/PhpstormProjects/alien_conquest/client/src/scenes/BaseGame.ts(73,47)
TS2339: Property 'socket' does not exist on type 'Game'.`
How can I fix this?
All Phaser classes declare their game property as type Phaser.Game. Even if the actual game instance is your custom Game, TypeScript can’t know that, so it reports an error because socket doesn’t exist on Phaser.Game.
If you’re instantiating your game using your custom class, this shouldn’t cause a runtime error. I myself don’t use TypeScript, so I’m not sure if there’s a way to tell it that you expect another class in place of Phaser.Game. The best advice I can give you, at least until someone else with more knowledge than me answers, would be to ignore the lines that reference this.game with a // @ts-ignore comment.
You are likely not getting the correct reference, as Telinc1 mentions. Really, if you could include the code that is trying to use Game.socket in another file, that would probably be more helpful. But if you are importing the right class or instance, it should work.