简体   繁体   English

将TypeScript模块视为对象

[英]Treat TypeScript modules as Objects

Well, this was working in Typescript 0.8.3 but as they said in "breaking changes" modules can only be used as namespaces. 好的,这在Typescript 0.8.3中有效,但是正如他们在“重大更改”中所说的那样,模块只能用作名称空间。
the javascript implementation of modules is just another object (closure). 模块的javascript实现只是另一个对象(关闭)。 So, is there any way to have the ability to treat imported modules as objects in TypeScript? 那么,有什么方法可以将导入的模块视为TypeScript中的对象?

Here is an example: 这是一个例子:

//Module.ts
export function SomeFunc(){
}

//app.ts
import moduleA = require("Module")
var anotherRef = moduleA;           //this is caught as error!

Using TypeScript 0.8.3 I could pass the anotherRef to any method or even Knockout bindings to use the module reference. 使用TypeScript 0.8.3,我可以将anotherRef传递给任何方法甚至Knockout绑定以使用模块引用。 but typescript 0.9.x prevents that. 但是打字稿0.9.x可以防止这种情况。

thanks 谢谢

This works fine for me in TS 0.9.0-1. 在TS 0.9.0-1中,这对我来说效果很好。 The limitation you mention is for internal modules and it does not affect external modules (amd/commonjs) like the one you have. 您提到的限制是针对内部模块的,它不会像您所拥有的那样影响外部模块(amd / commonjs)。 ie it is only for modules defined with the module keyword as: 即,仅适用于将模块关键字定义为的模块:

module SomeMod{
    export var foo = 123;  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM