TypeScript: module augmentation impossible

Due to typescript bug microsoft/TypeScript#18877

It is impossible to extend THREE.js class prototypes if they’re imported via Three.d.ts (i.e. import { Object3D } from "three";) using the method described here: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation

This has been brought up before, but as a a support question (#13425). I believe this is actually something that needs resolving through changes to Three, so have re-opened.

It looks as though the best solution to this as far as I can tell, is exporting an explicit list of names from Three.d.ts like: export { Object3DIdCount, Object3D } from './core/Object3D'; (which does work fine if I modify the .d.ts in my node_modules)

Unfortunately this bug in tsc was closed as a design limitation: microsoft/TypeScript#9532

I want to know if you would be on board with exporting every name explicitly from Three.d.ts, and how you would prefer to test that it works. I’m then happy to write a PR 🙂

My suggestions are making it fully automatic, as a part of:

EDIT: Changed to clarify the problem.

1 possible answer(s) on “TypeScript: module augmentation impossible

  1. Thanks, that is clearer yes. I’m still confused about why this is the case in TypeScript, but perhaps that is beside the point.

    Unfortunately, I don’t think we are able to maintain the more explicit exports in the way you suggest. That creates a fair bit more complexity managing files that are already quite difficult to manage and test.

    Could I suggest that you consider other options here? For example:

    A. Extend Object3D with inheritance, rather than modifying the prototype.
    B. Attach extra methods to a shared ObjectUtils class, or perhaps object.userData (which is not shared between instances, though).