简体   繁体   English

无法将JavaScript注入模式转换为TypeScript

[英]Unable to convert JavaScript injection pattern to TypeScript

We use a bespoke injection pattern in a rather large vanilla JavaScript application (more a framework with many consumers). 我们在相当大的原始JavaScript应用程序(更多具有许多使用者的框架)中使用定制的注入模式。 The pattern allows us to be explicit about what dependencies each JS module takes. 该模式使我们可以清楚地了解每个JS模块所采用的依赖性。 This is beneficial for two main reasons: it is easy to stub dependencies when unit testing a module and it allows our consumers to dynamically replace module implementations at runtime. 这有两个主要原因,这是有好处的:在对模块进行单元测试时,很容易对依赖项进行存根;它允许我们的使用者在运行时动态替换模块实现。

We have started migrating all of our modules over to TypeScript but have run into trouble converting a few modules that expose custom types. 我们已经开始将所有模块迁移到TypeScript,但是在转换一些公开自定义类型的模块时遇到了麻烦。 The problem is that TypeScript seems to want custom types to be exported immediately but we actually want to export the type after supplying the runtime dependencies via a function call (our dependency injection pattern). 问题在于TypeScript似乎希望立即导出自定义类型,但实际上我们希望通过函数调用(我们的依赖项注入模式)提供运行时依赖项后,导出类型。

So the question is how can we convert the following injection pattern to TypeScript without compromising the pattern itself? 因此,问题是如何在不损害模式本身的情况下将以下注入模式转换为TypeScript?

 module.exports = function ContextModule(logger) { var log = logger.register('context'); function Context() { log('creating type'); } Context.prototype = { doSomething: function() { log('doing something'); } } Context.doSomethingStatic = function() { log('doing something static'); } return Context; }; 

We return the custom type and can then new it up when we need to. 我们返回自定义类型,然后可以在需要时对其进行更新。 For example, we might compose the above highly contrived module in a main.js as follows: 例如,我们可以按以下方式在main.js中组成上述高度人为的模块:

 var loggerModule = require('LoggerModule.js'); var contextModule = require('ContextModule.js'); logger = loggerModule(); var Context = contextModule(logger); // then we might use the Context type var context = new Context(); context.doSomething(); // instance method Context.doSomethingStatic(); // static method 

Note the following: 请注意以下几点:

  • the logger module is injected into the context module 记录器模块被注入到上下文模块中
  • the logger module methods are then used in the ContextModule function directly or in the "class" implementation it is defining 然后可以直接在ContextModule函数中或在其定义的“类”实现中使用logger模块的方法
  • we use CommonJs to "require in" the module files and browserify to combine them 我们使用CommonJs“要求”模块文件,并通过浏览器进行组合

So the question is how can we convert the following injection pattern to TypeScript without compromising the pattern itself 所以问题是如何在不损害模式本身的情况下将以下注入模式转换为TypeScript

You can definitely use it as it is (JavaScript is valid TypeScript). 您绝对可以按原样使用它(JavaScript是有效的TypeScript)。 If you want safety, the process to convert the injection framework to TypeScript idioms is going to be more hard than just using one designed for it. 如果您需要安全性,那么将注入框架转换为TypeScript习惯用语的过程将比仅使用专门为其设计的过程更加困难。

I highly recommend https://github.com/inversify/InversifyJS 我强烈推荐https://github.com/inversify/InversifyJS

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

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