Skip to content
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

Closed
acrazing opened this Issue Sep 4, 2017 · 5 comments

Comments

Projects
None yet
3 participants
@acrazing
Copy link

acrazing commented Sep 4, 2017

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?

@kitsonk

This comment has been minimized.

Copy link
Contributor

kitsonk commented Sep 4, 2017

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.

@acrazing

This comment has been minimized.

Copy link
Author

acrazing commented Sep 4, 2017

@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?

@kitsonk

This comment has been minimized.

Copy link
Contributor

kitsonk commented Sep 4, 2017

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 its package.json contains types 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.

@acrazing

This comment has been minimized.

Copy link
Author

acrazing commented Sep 4, 2017

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.

@acrazing acrazing closed this Sep 4, 2017

@kitsonk

This comment has been minimized.

Copy link
Contributor

kitsonk commented Sep 4, 2017

That is what the types compiler flag/configuration option is for, as stated above.

@mhegazy mhegazy added the Question label Sep 5, 2017

@Microsoft Microsoft locked and limited conversation to collaborators Jun 14, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.