【发布时间】:2013-08-09 16:55:40
【问题描述】:
TypeScript 动态加载模块的方式是什么(模块的路径在运行时已知)?我试过这个:
var x = "someplace"
import a = module(x)
但似乎 TypeScript 编译器希望在编译时将路径视为 import/module 中的字符串:
$ tsc test.ts
/tmp/test.ts(2,19): error TS1003: Identifier expected.
/tmp/test.ts(2,20): error TS1005: ';' expected.
我知道我可以例如直接使用 RequireJS(如果我使用 amd 模块格式),但这对我来说感觉不对 - 它是针对特定库的解决方案。
【问题讨论】:
-
使用 TypeScript 0.9.1 而不是 'module' 您现在需要使用 'require' 尝试将导入更改为: import a = require(x)
-
@hnuecke:你的意思是
const a = require('x')?
标签: import module typescript