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

1 Answer 1

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[];
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.