简体   繁体   English

如何混合环境和非环境打字稿定义文件

[英]How to mix ambient and non-ambient typescript definition files

I have an older typescript project which uses the /typings/tsd.json method of handling .d.ts files, and which also uses Typescript's module keyword to compile source into javascript's IIFE module pattern. 我有一个较旧的Typescript项目,该项目使用/typings/tsd.json方法处理.d.ts文件,并且还使用Typescript的module关键字将源代码编译为javascript的IIFE模块模式。 The module setting in tsconfig.json (commonjs/amd/etc.) is ignored by that keyword. tsconfig.json(commonjs / amd / etc。)中的模块设置被该关键字忽略。

For a demo, I am adding to this project some newer typescript code which uses the more typical method of import and export keywords with a SystemJS module loader, with .d.ts files in /node_modules/@types/. 对于演示,我要向该项目添加一些较新的打字稿代码,该代码使用SystemJS模块加载器和/ node_modules / @ types /中的.d.ts文件使用更典型的关键字importexport方法。

After some Gulp/SystemJS gymnastics, it all works together at runtime and I'm on track to hit my deadline. 经过Gulp / SystemJS的一些体操训练后,它们全部在运行时一起工作,我有望按时完成任务。 But I am having trouble at compile-time in one scenario which I'd like to solve. 但是在一种我想解决的场景中,我在编译时遇到了麻烦。

When I modify the older code to use a class (a model) from the newer code, I'd like the old code to know about the .d.ts of the new code. 当我修改旧代码以使用新代码中的类(模型)时,我希望旧代码了解新代码的.d.ts。 So I added to the top of the old code's file /// <reference path="../../newercode/feature4/models/NewerModels.d.ts"/> . 因此,我在旧代码文件的顶部添加了/// <reference path="../../newercode/feature4/models/NewerModels.d.ts"/> (Alternately, I put the same line in tsd.d.ts but I get the same result.) (或者,我在tsd.d.ts中放入了同一行,但得到的结果相同。)

The compiler complains my old code "has or is using private name 'NewModel'". 编译器抱怨我的旧代码“已经或正在使用私有名称'NewModel'”。

Inside NewerModels.d.ts the import and export keywords are still there, while none of the .d.ts files already in /typings/ use those keywords. 在NewerModels.d.ts内部, importexport关键字仍然存在,而/ typings /中已经没有.d.ts文件使用这些关键字。 Those keywords are the culprit for the spurious compiler error. 这些关键字是伪造的编译器错误的元凶。

The legacy project wants ambient .d.ts files, newer code produces non-ambient. 旧项目需要环境.d.ts文件,而较新的代码将产生非环境文件。

Is there anything I can do about it? 有什么我可以做的吗?

My current solution isn't ideal, but it works for demo purposes. 我当前的解决方案并不理想,但是可以用于演示目的。

I create a new folder under /typings/ , called /feature4/ , and copy the relevant .d.ts files into there. 我在/ typings /下创建一个名为/ feature4 /的新文件夹,并将相关的.d.ts文件复制到该文件夹中 Edit them to remove export keywords, modify as needed to remove import keywords, and replace the class keyword with interface to prevent the patched legacy code from attempting a raw new NewerModel() call. 编辑它们以删除export关键字,根据需要进行修改以删除import关键字,并将class关键字替换为interface以防止修补的旧代码尝试尝试原始的new NewerModel()调用。

That last bit obviously wouldn't work because the newer classes don't actually exist on the global namespace, and the change from class to interface helps discourage this. 最后一点显然是行不通的,因为新的类实际上并不存在于全局名称空间中,并且从classinterface的更改有助于阻止这种情况。 Since I'm mainly grabbing models, an interface loses no functionality other than requiring new NewerModel() be written as <NewerModel>{} , but I still get compile-time checking on the properties of the model, which is 95% of what I wanted anyway. 因为我主要是抓模型,所以除了new NewerModel()编写为<NewerModel>{} ,接口不会失去任何功能,但是我仍然可以对模型的属性进行编译时检查,这是模型的95%无论如何我都想要。

I also have a few services that are treated the same way, but calling methods on them still work due to Dependency Injection. 我也有一些服务的处理方式相同,但是由于依赖注入,在它们上调用方法仍然有效。 The services were never called from the global namespace to begin with. 从来没有从全局名称空间调用这些服务。

This solution will obviously have a problem if someone changes /newercode/feature4/models/NewerModels.ts , but 1) there shouldn't be any between now and the deadline, and 2) if it's really an issue I could create a Gulp task to automate the copying and editing of the .d.ts files on each build. 如果有人更改/newercode/feature4/models/NewerModels.ts ,则此解决方案显然会出现问题,但是1)从现在到截止日期应该不存在任何问题; 2)如果确实存在问题,我可以创建一个Gulp任务自动复制和编辑每个版本上的.d.ts文件。

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

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