-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Re-export module namespace as global #14051
Comments
I am not sure i understand the issue, so this might be useless to you, but if |
The declaration file does have "export as namespace CodeMirror", but that does not help. The to-be-upgraded user code is already written as modules (so it doesn't see the global codemirror) but does not have an import of CodeMirror (so it doesn't get the module one either). |
Smaller repro of the problem.
Currently these don't compile together as written, and we want to eventually migrate all the various different code that looks like #3 to use an import statement. But in the interim we can temporarily modify #1, and I can't find an invocation that works in a way that allows us to incrementally migrate #3. |
How about something like this? Have a file that declares all the globals (assuming I'm reading the requirement right). import * as _foo from 'foo'
declare global {
var foo: typeof _foo;
} |
Thanks for looking! That works to re-export the |
@evmar If you can't do that, it means the definition was written incorrectly and it's not really accessible anyway. Those definitions would need to be fixed up. Do you have an example of what doesn't work and I can point out where to fix it up for use as an external module? |
Here's an example of the nested namespaces in the d.ts: |
That looks fine, you should be able to access that by Wasn't meant to imply they were definitely wrong, but if you need to access types and you can't it means that type was accidentally hidden in the definition (e.g. by not exporting it). I've seen that done a lot and was all I meant by "wrong". E.g. // Wrong (`Bar` is never exported).
declare namespace X {}
declare interface Bar {}
export = X
// Better (`Bar` is part of the exported namespace, can be accessed externally).
declare namespace X {
export interface Bar {}
}
export = X |
@blakeembrey if you try the example snippet I posted above, you'll see it doesn't work with your I believe the TS syntax for aliasing the value+type+namespace is the |
Edit: I wrote a long comment here but it was wrong because I have failed at cut and paste, let me get back to you. |
I had a combination of failing other piece, but the critical part was that to augment this module, it appears you must
and not do
So, to summarize, to make a typings module into a global:
to instead say
Thanks for your help, Blake! |
It doesn't work with |
Same problem - it doesn't reexport the namespace types. |
I would like to re-discuss allowing import aliases in `declare global blocks: declare global {
import foo = N;
} |
This works for me: import * as foo from "./foo";
export { foo }; |
It's not very handy but working: // imports.d.ts
import * as fs from 'fs';
export import fs = fs;
export as namespace imports; // global.d.ts
import fs = imports.fs; // another .ts file
fs.mkdtempSync('temp');
let stream: fs.ReadStream; |
@fightingcat Can you explain the syntax at all ? I thought as soon as you import a module it is scoped to the module, but I am able to reference this module as if it was ambient. Why ? I am using your workaround with dataloader to re-export members that I want availability globally. I may be passing around options for a dataloader in a module that doesn't actually create the dataloader so not reference the dataloader module explicitly.
|
@MicahRamirez This is UMD module definition, which can be used as a module or through a global variable. |
We're attempting to migrate users within Google to new @types typings, which move most typings into modules, and having a ton of trouble. The basic problem is that a module (e.g. CodeMirror) used to declare a global complex interface:
and now they are modules:
and other typings refer to them as modules (so we can't just undo that export statement) but we also have tons of older user code that doesn't refer to them as modules. Ideally we'd be able to make it work both ways while we migrate users incrementally.
I appreciate that you don't want the language to make this convenient, but I'm having difficulty finding any way to make it work temporarily while we migrate.
Some things I tried:
(and other variants) fails with "Imports are not permitted in module augmentations.".
If you wrap the file contents with a "declare global {", then I couldn't figure out a way to repackage that global as a module again. The
export = CodeMirror;
fails with "Cannot find name 'CodeMirror'." despite the global CodeMirror being declared in the same file.Do you have any advice? Note that "export as namespace" doesn't help because our user code is already modules, not scripts.
The text was updated successfully, but these errors were encountered: