找不到名称 "module"

cannot find the name "module"

我正在尝试 运行 一个简单的打字稿文件,我在其中导出一个名为 sum 的函数,如下所示:

我正在编写节点脚本。

function sum(a:number):number{
  return a;
}
module.exports.sum=sum;

我不明白我做错了什么。

我写了这个简单的脚本来理解单元测试用例。我想如果这个文件 运行ning 正确,那么我将使用 mocha 和 chai 开始基本测试用例。

下面是我的测试代码:

"use strict"

// Import chai.
let chai = require('chai'),
    path = require('path');

chai.should();

let SampleTest = require(path.join(__dirname, '..', 'sample.js'));

describe('Sampletesting', () => {
    describe('function sum', function(){
        it('should return number', function(){
            sum(1).should.equal(1);
        })
    })
});

这段代码对我来说工作正常;

假设我们有一个名为 math_utils.ts 的模块,它导出一个名为 foo 的函数和一个名为 obj

的对象
 // typescript syntax for exporting 
 export function foo(a: number): number {
    // whatever
 }

 export const obj = { life: 42 }

 const _aPrivateObjectToTheModule = {}

 function _aPrivateFunctionToTheModule() {}

现在我们在同一文件夹中定义另一个文件,例如math_utils.spec.ts,它将导入我们的模块。

import { should } from 'chai'
import { foo } from './math_utils'
// now we are able to call math_utils.foo()

describe('foo', () => {
    it('should....', () => {
        foo(1).should.equal(1);
    })
})

现在,仅作总结,在打字稿中,您可以通过这种方式导入模块成员...或按以下方式导入整个模块:

import * as chai from 'chai'
import * as MathUtils from './math_utils' 
// now in an object called MathUtils we have every member defined with the keyword export
const should = chai.should

describe('foo', () => {
    it('should....', () => {
        MathUtils.foo(1).should.equal(1);
    })
})
describe('obj', ()=> {
   // ...
   MathUtils.obj
})