Skip to content

fastify-socket.io: TS2339 'io' does not exist on type 'FastifyInstance' with Fastify v5.6.2 #311

Description

@UBU11

When using Fastify v5.6.2 with TypeScript and fastify-socket.io, the io property is not recognized by TypeScript, even though it exists at runtime.

Error:

error TS2339: Property 'io' does not exist on type 'FastifyInstance<Server<typeof IncomingMessage, typeof ServerResponse>, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault> & PromiseLike<...> & { ...; }'

error reproduce:

import fastify from "fastify";
import fastifyIO from "fastify-socket.io";

const server = fastify();
server.register(fastifyIO);

server.get("/", (req, reply) => {
  server.io.emit("hello");
});

server.ready().then(() => {
  // we need to wait for the server to be ready, else `server.io` is undefined
  server.io.on("connection", (socket) => {
    // ...
  });
});

server.listen({ port: 3000 });

official socket.io docs: link

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions