Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use import with "declare" keyword in TypeScript?

I have a d.ts file with a variable declaration, like this:

declare var $: () => SomeValue;

And it is works good, in other places i can use this variable without import.

But, when i add some import from another module, this variable is not visible from other code.

import { SomeValue } from "./SomeModule";
declare var $: () => SomeValue;

What the syntax for this need?

like image 881
Alisa Avatar asked Oct 28 '25 13:10

Alisa


1 Answers

When .d.ts files use export or import, they become treated as a module instead of as ambient typings.

However you can still add to the global namespace using declare global. This allows you to augment global types, and even add new global types:

.d.ts

import { SomeValue } from "./SomeModule";

declare global {
    interface Window {
        $: () => SomeValue;
    }
    interface SomeGlobalInterface {
        x: number;
    }
}

.ts

// () => SomeValue
window.$

let value!: SomeGlobalInterface;
value.x;
like image 63
yeerk Avatar answered Oct 31 '25 04:10

yeerk