fastify / fastify-websocket

basic websocket support for fastify
MIT License
394 stars 72 forks source link

WebSocket type is resolved to any using moduleResolution: "bundler" and ESM #258

Closed adjsky closed 1 year ago

adjsky commented 1 year ago

Prerequisites

Fastify version

4.17.0

Plugin version

8.0.0

Node.js version

16.20.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Pop!_OS 22.04

Description

image

The problem is in the d.ts file. The WebSocket is imported as import * as WebSocket from 'ws', but ws has 2 different d.ts files: the regular one and .mts one, the regular one has a single export export = WebSocket, but the .mts one exports functions, types and the WebSocket itself, resulting in type WebSocket = /*unresolved*/ any. I haven't seen this problem using "moduleResolution": "node".

Just by changing import * as WebSocket from 'ws' to import WebSocket from 'ws' the problem resolves. If that's okay i can PR fixes.

Steps to Reproduce

  1. Clone repository
  2. Run pnpm install
  3. See no error at line 10 in index.ts
  4. Change moduleResolution to node in tsconfig.json
  5. See an error at line 10 in index.ts
  6. Change moduleResolution to bundler in tsconfig.json
  7. Change import * as WebSocket from 'ws' to import WebSocket from 'ws' in the d.ts file
  8. See an error at line 10 in index.ts

Expected Behavior

The WebSocket type doesn't resolve to /*unresolved*/ any using ESM and moduleResolution: "bundler".

airhorns commented 1 year ago

That change makes sense to me if it works both ways!