Search code examples
node.jstypescriptdefinitelytyped

How to use Node.js `cluster` module with DT @types/[email protected] and TypeScript/commonjs?


I maintain a TS app using Node's cluster API that built fine with Node 14.x and Node 14.x cluster types from DT.

As I move this app to Node 16.x, I bumped @types/node from 14.x to 16.x, and then TS fails to compile:

  • Looking at the .d.ts (both its contents and the first doc lines at the top of the file), it expects me to use a default import: import cluster from 'cluster'; ...
  • ... whereas my TS code currently import * as cluster from 'cluster'; (no default import)

At that point,

  • If I move to a default import, TS compiles but my app is broken
  • If I stick to my import * as ... non-default imports, I have to add a bunch of // @ts-expect-error to be able to compile, and my app works.

→ What am I supposed to do? I'm asking here before filing a bug on DT, because I understand I'm possibly in a weird place because DT seems to be moving to ESM, and I'm still using TS with commonjs (my tsconfig has "module": "commonjs", "moduleResolution": "node").

... but if I try to add an "esModuleInterop": true to my tsconfig, then I get a zillion other errors from various dependencies (this is a large app).

The problem is reproducible with cluster.d.ts' example at the top of the file. Here's a TS Playground link.

→ Outcomes:

  1. Am I doing something wrong? If so, why is cluster the only Node built-in module that exhibits the issue? (I'm still very much able to import * as process from 'process'; or import * as os from 'os'; for example!)
  2. Is "yup you need these ts-expect-error" the sad answer until my app is moved to ESM?
  3. Or is there something problematic with the current DT types and I should file a bug? EDIT ah, I found two DT discussions that confirm the issue: 1, 2

Solution

  • Well, this is a bug. I found two DT discussions that confirm the issue: 1, 2.

    Hadn't found them before writing this StackOverflow post because I was looking in DT Issues, but somehow they switched to Discussions.