Reputation: 51
I currently use two applications with the same code for models.
I would like to create and share a library model (Typescript
) which uses inheritance.
Example : Pet extends Author
.
In my current application (Angular, the new one) I need to add some prototypal
functions that belong in the app domain to my both classes Pet and Author.
Example :
pet.extension file
Pet.prototype[‘fetchUrl’]
author.extension file
Author.prototype[‘follow’]
So I create a file named pet.extension
which imports pet.class
where I add domain methods then I export and use these overloaded classes.
When I create a new instance I import this extension. No problem. I have my class (Pet) with extended prototypal
methods. However this extension(overloaded class) uses pet.class
and pet.class extends author.class
not author.extension
Do you know any way to create and use an overloaded version of Author?
The ultimate goal in my mind would be to simulate an equivalent of extensions in Swift language. To keep in my library the oop structure.
I hope I’m clear :)
Thanks a lot for your help.
Upvotes: 3
Views: 2149
Reputation: 51
Relative to my previous post, I finally found the exact term of what I am trying to do. A module augmentation.
import { Model } from ‘my-library’;
declare module ‘my-library’ {
interface Model {
newMethod(): string
}
}
Model.prototype.newMethod = function() {
return ‘something’;
}
Here is an example. Visual studio code debugger returns the following error on Model from "Model.prototype.newMethod"
‘Model' only refers to a type, but is being used as a value here.
I tried to export my library from commonjs and amd, but getting the same issue.
So I tried to reproduce the same code available on Typescript documentation.
This time, no error displayed on Model Object but on assignation method :
'Property 'newMethod' does not exist on type ‘Model’.
I’m using typescript 2.1.6 for my library and 2.0.3 with angular.
It begins to drive me insane. Any ideas of what's wrong ?
Thanks a lot for your help.
Upvotes: 1