How does typescript define global variables?

for example, in the nodejs environment, there is a global variable global, which I defined directly when I didn"t use typescript before.
for example, redis instances and database instances are all defined as global instances. When it comes to typescript, we have no idea what to do with it.

Jun.10,2022

create a UUU.d.ts file (theoretically, of course, you can name it at will):

declare var  UUU:any

this creates a global variable UUU.

clipboard.png


I seem to have solved it myself.
I write the global object as a singleton pattern, and each module is introduced.

I found this answer before and felt dissatisfied: https://codeshelper.com/q/10...

.

there are better answers to add ~


Global variables should be avoided as much as possible. You can introduce instances using singleton mode, such as


    class DataMgr {
    static instance: DataMgr
    static getInstance() {
        if (!DataMgr.instance) {
            DataMgr.instance = new DataMgr()
        }
        return DataMgr.instance
    }

introduce this module when you want to use an instance, and DataMgr.getInstance () can

Menu