首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeScript和RequireJS -我的静态输入到哪里去了?

问TypeScript和RequireJS -我的静态输入到哪里去了?
EN

Stack Overflow用户
提问于 2014-03-15 03:46:00
回答 1查看 76关注 0票数 0

在TypeScript中,我可以使用RequireJS导入模块:

代码语言:javascript
复制
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,直到真正需要它时,比如调用某个函数:

代码语言:javascript
复制
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是什么类型?

EN

回答 1

Stack Overflow用户

发布于 2014-03-15 03:55:44

在Modules in TypeScript documentation中(请参阅“可选模块加载”):

示例: require.js中的动态模块加载

代码语言:javascript
复制
declare var require;
import Zip = require('./ZipCodeValidator');
if (needZipValidation) {
    require(['./ZipCodeValidator'], (x: typeof Zip) => {
        if (x.isAcceptable('...')) { /* ... */ }
    });
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22414401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档