microsoft / microsoft/TypeScript

Re-export module namespace as global

Open
#14,051 19 comments 19 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the two declaration patterns and the diagnostics quoted in the issue. Trace how export =, declare global, and export as namespace are handled, then determine whether a supported approach or language change can satisfy both module and global usage; done would require a clear, tested resolution.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.