Is there a way to properly type the private property private io
in Typescript to prevent the error message Property 'io' has no initializer and is not definitely assigned in the constructor
import fastify, { FastifyReply, FastifyRequest } from "fastify";
import socketIO from "socket.io";
import fastifysocketIO from "fastify-socket.io";
import fastifyStatic from "fastify-static";
import path from "path"
class App {
private server;
private port: number;
private io: socketIO.Server;
constructor(port: number) {
this.server = fastify({ logger: true });
this.port = port;
this.server.register(fastifyStatic, {
root: path.join(__dirname, "../public"),
});
this.server.register(fastifysocketIO, ({
cors: { origin: "*" }
}));
this.server.ready(err => {
if (err) { throw err }
this.io = this.server.io;
this.io.on('connect', (socket: socketIO.Socket) => {
return console.info('Socket connected!', socket.id);
})
});
this.server.get("/", (request: FastifyRequest, reply: FastifyReply) => {
reply.sendFile("chat.html");
})
}
public start() {
this.server.listen(this.port);
}
}
new App(3000).start();