How can I import TypeScript AMD modules without making the dependent code a module

时光毁灭记忆、已成空白 提交于 2019-12-06 01:39:15

The best way to handle this is to actually compile with the commonjs flag and use the fact that the TypeScript compiler doesn't actually emit require statements if the module import isn't used in a value position. This is similar to the "optional module loading" advanced scenario documented in the Modules in TypeScript page.

Sample code:

declare var require: any; // If needed
import _a = require('Dependency');
import _b = require('SomeOtherDependency');

var a: typeof _a = require('Dependency');
var b: typeof _b = require('SomeOtherDependency');

// Use 'a' and 'b' in your code. Do not use _a or _b.
a.doSomething();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!