在 Typescript 中如何使 class 定义可用

In Typescript how to do I make a class definition available

我是打字稿的新手,我正在尝试迁移现有项目。它是用 Durandal 编写的,因此使用了 AMD。

我的问题是如何使我编写的 类 的定义可供其他文件使用,例如参数定义或接口。

再说清楚一点。我定义了一个 class 例如

class Example{
   constructor(){
     //some other code
   }
}
 export = Example

如果我想实例化 class 我用 require 导入它就可以了。

但是如果我只想在另一个文件中将它用作参数类型怎么办,例如

function(value:Example){
}

或者在界面中

interface exampleInterface{ 
   value:Example
}

我可以在第一个示例中使用 require 导入它,但这似乎不是正确的方法,因为我正在发出代码,而我所做的只是在编译时使用 class。如果我在第二个示例中使用 require,则该界面将不再对其他 classes.

可见

到目前为止我发现的唯一方法是在单独的文件中声明 class 的环境版本。这行得通,但显然是重复和维护问题。

有没有更好的方法?

谢谢

I can import it using require in the first example, but this doesn't seem like the correct way since I'm emitting code when all I'm doing is using the class for compile time

这是推荐的方式。基本上,它确保具有 class 定义的文件在运行时 此文件之前被 解析 。请注意,如果您不使用导入的东西作为变量,那么代码发出不会依赖

有关文档,请参阅 Import Type Only