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.

Allow augmentation of re-exported module

See original GitHub issue

I’ve sadly discovered that it not possible to augment a ES2015 module that is being re-exported. At least not without specifying the full path to the ES2015 that is then being re-exported.

This has significant implications, particularly for library authors and consumers.

An example would probably help clarify.

Suppose as an authored of a library I want to expose a simple surface area from which consumers should import from. I do this by creating an index.ts file that re-exports modules from nested sub-folders like so:

// index.ts
export { AClass } from './path/to/class-a';
export { BClass } from './path/to/class-b';
export { CClass } from './path/to/class-c';

Now a consumer can import like so:

import { AClass, BClass } from 'some-library'; 

The benefits of the above:

  1. As library author I am free to reorganise the modules into sub-folders as the library code base grows without breaking consumers
  2. As library consumer I don’t need to have intimate knowledge of how the library organises it’s file on disk - instead I have one path to import modules and this is the name of the npm package itself.

Once augmentation enters the picture all of the above benefits are now gone.

This is really bad for everyone.

What can be done about the situation?

Thanks Christian

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:10
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
RyanCavanaughcommented, Apr 1, 2019

The OP is slightly ambiguous so I’ll address both.

If the request is to allow the exporting library to create a new type that augments AClass, that’s a strong “no” due to our current architecture and potential user confusion.

If the request is that augmentors can augment the exported AClass type as if it were declared upfront in the exporting library, 100% yes, that “should” work (where “should” is defined as “it is effectively feature work to support each possible variant, but we are on board with someone contributing that work”). Effectively, augmenting any type or value through its aliases should be the same as augmenting the type or value at its original declaration site.

2reactions
speiggcommented, Oct 9, 2018

Ah, this is why it’s not working. I’m trying to do the same thing as @endel (augmenting a re-exported class from the package-scoped three module).

EDIT, this worked for me:

declare module 'three/three-core' {
    interface Object3D {/* augmentations */}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is a typescript module augmentation automatically exported in ...
I created a component library using material-ui and Typescript under the hood. I have used Typescript module augmentation in order to add ...
Read more >
Exported Module Augmentation : r/typescript - Reddit
When I want to define another interface for module A but in another file, let say the name of the interface is I2...
Read more >
Documentation - Declaration Merging - TypeScript
Module Augmentation. Although JavaScript modules do not support merging, you can patch existing objects by importing and then updating them. Let's look at...
Read more >
Solve any external library error in TypeScript with module ...
Before we dive into module augmentation, let's briefly cover how ... The reason is you need to augment an export by its exported...
Read more >
Module Augmentation in TypeScript - DigitalOcean
A demonstration of the creation and usage of TypeScript module augmentation, which allows us to extend the functionality of other modules.
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