在TypeScript中,我可以使用RequireJS导入模块:
import Foo = require("common/Foo"); // Foo is one of my TypeScript exported classes.
class Bar {
// Wooohooo! I can use Foo here, complete with intellisense!
new Foo(1, "ab").zanzibar();
}但有时我不想加载Foo,直到真正需要它时,比如调用某个函数:
class Bar {
doSomething() {
// OK, now we need Foo. Import it.
require(["common/Foo"], Foo => {
// Use Foo here.
// Uh oh. No intellisense -- where did my static typing disappear to?
new Foo(1, "ab").zanzibar(); // Works at runtime, but no intellisense. :-(
});
}
}有没有办法告诉TypeScript Foo是什么类型?
发布于 2014-03-15 03:55:44
在Modules in TypeScript documentation中(请参阅“可选模块加载”):
示例: require.js中的动态模块加载
declare var require;
import Zip = require('./ZipCodeValidator');
if (needZipValidation) {
require(['./ZipCodeValidator'], (x: typeof Zip) => {
if (x.isAcceptable('...')) { /* ... */ }
});
}https://stackoverflow.com/questions/22414401
复制相似问题