gj / fastify-ws

MIT License
55 stars 11 forks source link

Problems using it with TypeScript #2

Open kristianmandrup opened 6 years ago

kristianmandrup commented 6 years ago
fastify.ready(err => {
  if (err) throw err

  fastify.ws
    .on('connection', socket => {
      socket.on('message', msg => socket.send(msg)) // Creates an echo server
    })
})

Errors

[ts] Argument of type '(err: any) => void' is not assignable to parameter of type '() => void'.
(parameter) err: any

[ts] Property 'ws' does not exist on type 'FastifyInstance<{}, {}, {}>'.

Had to tweak it to the following to make it compile

fastify.ready(() => {
  fastify['ws']
    .on('connection', socket => {
      socket.on('message', msg => socket.send(msg)) // Creates an echo server
    })
})
gj commented 6 years ago

Interesting; if I ever get a free moment to mess around with TS, I'll look into this. Or if anyone else wants to put forward an explanation, that'd be awesome!

Thanks for pointing this out @kristianmandrup !

jerome-nelson commented 5 years ago

Late reply but related. This is due to the FastifyInstance interface in fastify npm package not having ws defined.

https://github.com/fastify/fastify/blob/78bef60c97c7ade1cd491569302a706b9df613bc/fastify.d.ts#L343

As specified on the TypeScript website (https://www.typescriptlang.org/docs/handbook/modules.html):

If you don’t want to take the time to write out declarations before using a new module, you can use a shorthand declaration to get started quickly.

declarations.d.ts
declare module "hot-new-module";