Skip to content

Instantly share code, notes, and snippets.

@benhatsor
Last active May 8, 2026 17:34
Show Gist options
  • Select an option

  • Save benhatsor/1c59c958ae0d7b3f75ca87a4bc6cf276 to your computer and use it in GitHub Desktop.

Select an option

Save benhatsor/1c59c958ae0d7b3f75ca87a4bc6cf276 to your computer and use it in GitHub Desktop.
Proposal: Granular import with module namespace

Proposal: Granular import with module namespace

Barrel import with module namespace:
Problem: loses granular intent as well as potential tree-shaking benefits.

import * as util from './util'

Granular import without module namespace:
Problem: loses scope, conflicts with other local or imported module functions.

import { concurrent, stream, regex } from './util'

Often, these conflicts force verbose remappings, such as:

import { concurrent as concurrentUtils,
         stream as streamUtils,
         regex as regexUtils } from './util'

Proposed syntax: keeps the scope and organization of a module namespace while preserving granular imports.

import { concurrent, stream, regex } as util from './util';
//                                   ^^^^^^^

Called as:

util.concurrent.someFunc(...)

No good alternative exists, even using dynamic imports:

let util = await import('./util')
util = { concurrent: util.concurrent, stream: util.stream, regex: util.regex }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment