首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Typescript中使用'nodeunit‘模块

在Typescript中使用'nodeunit‘模块
EN

Stack Overflow用户
提问于 2015-05-29 09:23:47
回答 1查看 363关注 0票数 0

DefinitelyTyped有一个type definition file for nodeunit

在其中,有

代码语言:javascript
复制
declare module 'nodeunit' {
    export interface Test {
    }
}

如何在我的代码中使用它?看起来小写的拼写造成了一些麻烦。

以下选项均不起作用:

代码语言:javascript
复制
/// <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

EN

回答 1

Stack Overflow用户

发布于 2015-05-29 09:37:25

declare module 'nodeunit'意味着我们已经将它声明为一个外部模块。您可以使用以下命令导入它:

代码语言:javascript
复制
import nodeunit = require('nodeunit');

并使用--module commonjs进行编译。

另外,您还可以在测试文件中查看定义的示例用法,在本例中为:https://github.com/borisyankov/DefinitelyTyped/blob/master/nodeunit/nodeunit-tests.ts

更新

基于:

,但我不想使用require。该文件不包含任何实现,仅包含类型定义

我假设您自己声明了一个外部模块。在这种情况下,您将执行以下操作:

代码语言:javascript
复制
declare module "mine" { 
    import nodeunit = require('nodeunit');

    function testX(test: nodeunit.Test);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30519662

复制
相关文章

相似问题

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