So the extension methods discussion has been closed. Which, maybe the mechanism is hard to automate with JavaScript semantics. There does however still appear to be a gap with how TypeScript can accomodate existing JavaScript behaviour with prototypes:
If I do the following:
import { MyType } from "./MyType";
MyType.prototype.newMethod = () => { /* ... */ };
It seems like presently, there's no way to tell TypeScript that from now on, anything that imports that type will have newMethod available.
It seems like there should be some way for TypeScript to just automatically know this based on whether the consuming module imports the module that has performed the modification:
import { MyType} from "./MyType";
const blah = new MyType();
blah.existingMethod();
blah.newMethod(); // Will never type check, but could work depending on import order, not good.
So, as a rule to make this work:
import { MyType } from "./MyType";
import "./MyTypeExtension"; // Imagine this contains the first code block in this ticket.
const blah = new MyType();
blah.existingMethod();
blah.newMethod(); // Type checks *and* always works!
It seems like so long as the module consuming any kind of prototype modification pulls in the module that performs the modification TypeScript can count on the new method existing.
I've read up on declaration merging and have yet to successfully use it, or it might be that it's not useful for addressing this scenario.
The main reason why this feature would be nice is so that I don't have to force people down the road of writing any kind of boilerplate or inherit/compose multiple base classes I need to offer to obtain base functionality. I can elaborate on this some more in a subsequent comment if desired, but it's very much a similar thing to extension methods in C#, or traits and interfaces in PHP.
So the extension methods discussion has been closed. Which, maybe the mechanism is hard to automate with JavaScript semantics. There does however still appear to be a gap with how TypeScript can accomodate existing JavaScript behaviour with prototypes:
If I do the following:
It seems like presently, there's no way to tell TypeScript that from now on, anything that imports that type will have
newMethodavailable.It seems like there should be some way for TypeScript to just automatically know this based on whether the consuming module imports the module that has performed the modification:
So, as a rule to make this work:
It seems like so long as the module consuming any kind of prototype modification pulls in the module that performs the modification TypeScript can count on the new method existing.
I've read up on declaration merging and have yet to successfully use it, or it might be that it's not useful for addressing this scenario.
The main reason why this feature would be nice is so that I don't have to force people down the road of writing any kind of boilerplate or inherit/compose multiple base classes I need to offer to obtain base functionality. I can elaborate on this some more in a subsequent comment if desired, but it's very much a similar thing to extension methods in C#, or traits and interfaces in PHP.