简体   繁体   中英

How to override library models that belong to app domain in Typescript [Module augmentation]?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM