-
Notifications
You must be signed in to change notification settings - Fork 12.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to define a global variable in a typescript module, which could be used in other modules directly without import? #18237
Comments
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 An example would be something like: declare const foo: string; And then |
@kitsonk I found this problem could be resolved by create a file in current module, and declare a import statement as For example: We published a module named as export {}
declare global {
const foo: string
} And then we are writing a new module named as import 'foo' And then we can call This is complex. How about let compiler scans all directories in |
I gave you instructions. Do not use the There is also the ability to use triple-slash references.
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 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 |
Currently, we have two ways to define a global variable, the first one is use
@types
define it, and the second one is usedeclare 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?The text was updated successfully, but these errors were encountered: