TypeScript和RequireJS-where';我的静态打字走了吗?
在TypeScript中,我可以使用RequireJS导入模块:TypeScript和RequireJS-where';我的静态打字走了吗?,requirejs,typescript,Requirejs,Typescript,在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,
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是什么类型的 从(参见“可选模块加载”):
示例:require.js中的动态模块加载
declare var require;
import Zip = require('./ZipCodeValidator');
if (needZipValidation) {
require(['./ZipCodeValidator'], (x: typeof Zip) => {
if (x.isAcceptable('...')) { /* ... */ }
});
}