starcorn
starcorn

Reputation: 8531

How to declare a global, globalThis in Typescript?

I've been googling to find some updated answer on how to declare globals in Typescript. But I'm not sure which tutorial/guide has the most updated solution.

Anyhow I'm trying to get my custom global declares d.ts to work. Right now I've setup so that my Typescript project is built and bundled using webpack/ts-loader.

My tsconfig.json file looks as following. Guides I've read said things that I don't really need to set typeRoots as Typescript can find the types by it self. I've tried with and without anyhow, and both resulted in the same.

{
    "files": [
        "app.ts",
    ],
    "compilerOptions": {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "module": "commonjs",
        "moduleResolution": "node",
        "noImplicitAny": false,
        "target": "es5",
        "declaration": false,
        "sourceMap": true,
        "skipLibCheck": true,
        "lib": ["es6", "dom"],
        "esModuleInterop": true,
        "typeRoots": [
            "./typings/",
            "../../../../../node_modules/@types/"
        ]
    }
}

And my d.ts file which I named to soho.d.ts contains the following

declare global {
    namespace Soho {

    }
    interface Window {
        Soho: Soho;
    }

    interface Soho {
        Locale: any;
    }
}

I get the following error though in webpack

TS2304: Cannot find name 'Soho'

In the code I'm trying to use my global as following

Soho.Locale

Did I set my tsconfig.json file correct, or is it something wrong with my d.ts file?

Upvotes: 0

Views: 4159

Answers (1)

starcorn
starcorn

Reputation: 8531

There's an error in my tsconfig.json file. And the error is in the specified files property which tells Typescript to only use that file during compilation.

Therefore my custom d.ts files where omitted thus the errors.

So as for the solution. Either remove files property or include the folder with custom d.ts by using the include property.

Upvotes: 2

Related Questions