# Proposal: re-export declaration merging?

**URL:** <https://es.discourse.group/t/proposal-re-export-declaration-merging/1737>\
**Category:** 💡 Ideas\
**Created:** [June 16, 2023, 7:29pm UTC](https://es.discourse.group/t/proposal-re-export-declaration-merging/1737 "2023-06-16T19:29:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trusktr](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/trusktr/32/183_2.png) [@trusktr](https://es.discourse.group/u/trusktr)\
**Post date:** [June 16, 2023, 7:29pm UTC](https://es.discourse.group/t/proposal-re-export-declaration-merging/1737/1 "2023-06-16T19:29:33Z")

</div>

For example:

```js
// some-lib/index.js
export * as w from './compiler.js';
export * as w from './methods.js';
export * as w from './module.js';
export * as w from './nodes.js';
export * as w from './utils.js';

```

and the consumer can:

```js
import {w} from 'some-lib/index.js'

```

Benefit: `w` is auto-importable in IDEs in this manner.

Otherwise, with only this:

```js
// some-lib/index.js
export * from './compiler.js';
export * from './methods.js';
export * from './module.js';
export * from './nodes.js';
export * from './utils.js';

```

the consumer _can_ do this:

```js
import * as w from 'some-lib/index.js'

```

but they cannot type `w` and have their IDE suggest importing `w` from `some-lib`.

With small libs, being able to auto-import the lib's index is more useful than tree-shaking (exporting everyting under a variable makes it more difficult to tree shake).

---

<div class="post-metadata">

**Author:** ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)\
**Post date:** [June 16, 2023, 8:22pm UTC](https://es.discourse.group/t/proposal-re-export-declaration-merging/1737/2 "2023-06-16T20:22:33Z")

</div>

You can accomplish this with an intermediate file:

```js
// some-lib/all-exports.js
export * from './compiler.js';
export * from './methods.js';
export * from './module.js';
export * from './nodes.js';
export * from './utils.js';

```

```js
// some-lib/index.js
export * as w from './all-exports.js';

```

It's a little awkward, but I think not so bad or common that it warrants introducing a magic merging behavior on what would otherwise be an identifier conflict.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [June 16, 2023, 10:36pm UTC](https://es.discourse.group/t/proposal-re-export-declaration-merging/1737/3 "2023-06-16T22:36:00Z")

</div>

You could even do this with a single file, though that's probably far more awkward:

```js
// some-lib/index.js
export * from './compiler.js';
export * from './methods.js';
export * from './module.js';
export * from './nodes.js';
export * from './utils.js';

import * as w from './index.js'; // self-import
export { w }

```

But I agree, "namespace object merging" should not be necessary.

> [@trusktr](#):
>
> Benefit: `w` is auto-importable in IDEs in this manner.

I'd rather see IDEs offer an auto-import when you type `someLib`. You still can choose to rename that local variable to `w` later. I don't like modules that export namespace objects when named exports would have been sufficient.

> [@trusktr](#):
>
> exporting everyting under a variable makes it more difficult to tree shake

All bundlers I've seen handle namespace objects just fine, as long as you only access individual properties on them (instead of treating them as objects and, say, enumerate their properties or something).

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [June 16, 2023, 11:42pm UTC](https://es.discourse.group/t/proposal-re-export-declaration-merging/1737/4 "2023-06-16T23:42:17Z")

</div>

> [@bergus](#):
>
> `import * as w from './index.js'; // self-import`

That just blew my mind
