question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Re-export module namespace as global

See original GitHub issue

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:

declare function CodeMirror(...);
declare namespace CodeMirror { ... }

and now they are modules:

export = CodeMirror;

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:

  1. Aliasing the module into the global namespace
declare global {
  import CodeMirror = require('codemirror');
}

(and other variants) fails with “Imports are not permitted in module augmentations.”.

  1. Making the module global first, then aliasing it into a module namespace. 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.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:16
  • Comments:19 (13 by maintainers)

github_iconTop GitHub Comments

11reactions
blakeembreycommented, Feb 13, 2017

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;
}
7reactions
fightingcatcommented, Jun 24, 2017
import * as _foo from 'foo'
declare global {
  var foo: typeof _foo;
}

It doesn’t work with let something: foo.SomeType, claim Cannot find namespace 'foo'.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Re-export namespace from external module to global in ...
I'm running into some problem exporting, like it's said on title, a namespace from an external module to the global namespace in a ......
Read more >
Documentation - Namespaces and Modules - TypeScript
How to organize code in TypeScript via modules or namespaces. ... Namespaces are simply named JavaScript objects in the global namespace.
Read more >
Modules • JavaScript for impatient programmers (ES2022 ...
some-module.mjs'; // Namespace import import * as someModule from './some-module.mjs' ... Scripts are code fragments that browsers run in global scope.
Read more >
Modules and Namespaces - Typescript - Dot Net Tricks
//importing the exporting types from reexport file import { st as ... Namespaces are simply named JavaScript objects in the global scope.
Read more >
Organizing TypeScript code using namespaces
Modules offer some additional benefits like strong code isolation, strong support for bundling, re-exporting of components, and renaming of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found