How to define a global variable in a typescript module, which could be used in other modules directly without import?
Closed this issue ยท 5 comments
Currently, we have two ways to define a global variable, the first one is use @types
define it, and the second one is use declare global
in a module. But the first one method need to publish to @types
, which is just for modules written in javascript, and the second one need to import to use it. Is there any way to define a variable in a typescript module which could be invoked in other modules directly?
This is not a support forum.
Questions should be asked at StackOverflow or on Gitter.im.
But, there are what are called ambient/global type definitions. They simply need to be in scope of the compiler (or included via a type reference). The declare global
was introduced when modular type definitions were introduced to model modules that augment the global namespace, so basically ambient declarations or the old way. By convention, these are often created as interfaces.d.ts.
An example would be something like:
declare const foo: string;
And then foo
would be part of the global namespace.
@kitsonk I found this problem could be resolved by create a file in current module, and declare a import statement as import 'xxx'
, the xxx
is the module that exports global variables. And the we can invoke global variables exported by xxx
module in current module's files.
For example:
We published a module named as foo
, and it declared a global variable as follow:
export {}
declare global {
const foo: string
}
And then we are writing a new module named as bar
, it dependents on the module foo
, if we call the global variable foo
, will throw an error, but if we declare a file in the module, just like globals.ts
as follow:
import 'foo'
And then we can call foo
in any files in module bar
.
This is complex. How about let compiler scans all directories in node_modules
, and check if its package.json
contains types
field or not, rather than only scan @types
directory?
I gave you instructions.
Do not use the export {}
or declare global
. I explained to you about ambient/global typings. They just need to be in the scope of your compiler path and by convention they are usually called interfaces.d.ts
. There then is no need for the import
.
There is also the ability to use triple-slash references.
How about let compiler scans all directories in
node_modules
, and check if itspackage.json
containstypes
field or not, rather than only scan@types
directory?
It already does this, but only if TypeScript can determine that you are using that package, and then it will follow the modular or ambient/global typings that are part of that package. If you are not directly importing a module from that package, but somehow the package is available at runtime, that is what the --types
compiler flag is for.
But again, this is not the the forum for asking questions.
Ok, I see your means. I will close this issue immediately. But I still wanna to know how should the module 'mocha' write its typings if it is a typescript module rather than javascript with @types. It exports some global functions like it, describe, etc. When we write a module dependents on it, and if we specify types compiler option, we need to include all modules in it.
That is what the types
compiler flag/configuration option is for, as stated above.