TypeScript Version: 2.5.2
Code
This can't be demonstrated with a simple 1-file code example so I made a trivial project to demo it:
https://github.com/chriseppstein/typescript_module_augmentation_bug
Instructions for how to run the demo and work around the issue are included on the README there.
When I include an interface in a node module's main index.ts file, then it is open for merging with a declare module "my_npm_module" {} but when I move that interface into a local submodule and export it in the main index.ts with a export * from "./local_module"; then the downstream consumer breaks.
Expected behavior:
It seems to me that the point of being able to export from other modules is to keep downstream consumers protected from internal refactors and maintain an existing public API. So I expect to be able to augment an interface and have it behave the same whether or not it has been re-exported.
Actual behavior:
It looks like the interface gets forked into two definitions, the downstream module sees only the interface surface that was added and the upstream module only sees the interface surface it defined.
This is a known limitation. See #9532 and its cross-referencing issues for more details.
The only workaround is to augment the module that directly exports the declaration which of course presents maintainability problems and diminishes the level of abstraction. I've run into this a number of times. I usually just take the risk, targeting the module directly in the augmentation, knowing it may well break in the future.
@aluanhaddad Thanks for the reply. I've moved the code that needs to be augmented to the root of the node module. Not ideal, to say the least.
Look like this is bug in TypeScript 3.3.3333 I found that when using export star will cause augmentation failed. Workaround is manually export like below
export { ClassA, ClassB, otherInstance, otherNamespace } from "my-cool-module"
@ktutnik Thanks for the solid workaround.
I've moved all my "augmentable" types to a file called augmentable.ts.
Then, in index.ts,
export {
TypeA,
TypeB,
TypeC,
//snip
} from "./augmentable";
All other types (generics, functions, etc) that rely on declaration merging/augmentation import from augmentable.ts.
It seems to work well so far.
Most helpful comment
Look like this is bug in TypeScript 3.3.3333 I found that when using export star will cause augmentation failed. Workaround is manually export like below