2

I am trying to extend the global in node.js, using typescript.

For this, I've created the following structure: enter image description here

File content of index.d.ts:

import socketio from 'socket.io';

declare namespace NodeJS {
    interface Global {
        sockets: socketio.Socket[];
    }
}

In my tsconfig.json file I provided: "typeRoots": [ "./node_modules/@types", "./@types"]

But I still can't use global.sockets. How to fix it?

Please note that if I would write index.d.ts as:

declare namespace NodeJS {
    interface Global {
        sockets: any[];
    }
}

Then I would be able to access global.sockets

T THE R
  • 195
  • 1
  • 13

1 Answers1

3

From this stackoverflow question: Ques

Turns out that adding export {} and declare global would do the work.

So the final file would be:

export {};

import socketio from 'socket.io';

declare global {
    declare namespace NodeJS {
        interface Global {
            sockets: socketio.Socket[];
        }
    }
}
Tal Rofe
  • 457
  • 12
  • 46