简体   繁体   English

如何在Typescript [模块扩充]中覆盖属于app域的库模型?

[英]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. 我想创建和共享一个使用继承的库模型( Typescript )。

Example : Pet extends Author . 示例: 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. 在我当前的应用程序(Angular,新的应用程序)中,我需要将属于app域的一些prototypal函数添加到我的Pet和Author类中。

Example : 示例:

pet.extension file pet.extension文件

Pet.prototype[‘fetchUrl’]

author.extension file author.extension文件

 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. 所以我创建了一个名为pet.extension的文件,它导入pet.class ,我在其中添加域方法,然后导出并使用这些重载的类。

When I create a new instance I import this extension. 当我创建一个新实例时,我导入了这个扩展。 No problem. 没问题。 I have my class (Pet) with extended prototypal methods. 我有我的班(宠物)与扩展的prototypal方法。 However this extension(overloaded class) uses pet.class and pet.class extends author.class not author.extension 但是这个扩展(重载类)使用pet.classpet.class extends author.class而不是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. 我脑海中的最终目标是模拟Swift语言中的等效扩展。 To keep in my library the oop structure. 在我的图书馆保留oop结构。

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" Visual Studio代码调试器从“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. 我试图从commonjs和amd导出我的库,但是遇到了同样的问题。

So I tried to reproduce the same code available on Typescript documentation . 所以我尝试重现Typescript文档中可用的相同代码。

This time, no error displayed on Model Object but on assignation method : 这次,模型对象上没有显示错误,但是在赋值方法上没有:

'Property 'newMethod' does not exist on type 'Model'. '属性'newMethod'在'Model'类型上不存在。

I'm using typescript 2.1.6 for my library and 2.0.3 with angular. 我正在使用typescript 2.1.6作为我的库,2.0.3使用angular。

It begins to drive me insane. 它开始让我疯狂。 Any ideas of what's wrong ? 有什么不妥的想法?

Thanks a lot for your help. 非常感谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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