DefinitelyTyped有一个type definition file for nodeunit。
在其中,有
declare module 'nodeunit' {
export interface Test {
}
}如何在我的代码中使用它?看起来小写的拼写造成了一些麻烦。
以下选项均不起作用:
/// <reference path='../nodeunit.d.ts' />
// cannot find name 'nodeunit'
function testX(test: nodeunit.Test){}
// syntax error
function testX(test: 'nodeunit'.Test){}
// signature with implementation cannot use string literal type
function testX(test: 'nodeunit.Test'){}
// cannot find name 'NodeUnit'
function testX(test: NodeUnit.Test){}如果我只删除模块声明中的引号,就可以使用第一个版本:declare module nodeunit。
发布于 2015-05-29 09:37:25
declare module 'nodeunit'意味着我们已经将它声明为一个外部模块。您可以使用以下命令导入它:
import nodeunit = require('nodeunit');并使用--module commonjs进行编译。
另外,您还可以在测试文件中查看定义的示例用法,在本例中为:https://github.com/borisyankov/DefinitelyTyped/blob/master/nodeunit/nodeunit-tests.ts
更新
基于:
,但我不想使用require。该文件不包含任何实现,仅包含类型定义
我假设您自己声明了一个外部模块。在这种情况下,您将执行以下操作:
declare module "mine" {
import nodeunit = require('nodeunit');
function testX(test: nodeunit.Test);
}https://stackoverflow.com/questions/30519662
复制相似问题