打字稿中的Node.js模块

模块代码(在打字稿中书写)看起来像是能够像这样使用它:

/// <reference path="./onoff.ts" /> //import * as onoff from "./onoff"; var onoff = require("./onoff"); var app = onoff(); var computers: Onoff.Computer[] = []; app.start(computers); 

我确定它一定是这个,但它不起作用:

 import * as express from "express"; export module Onoff { export class Computer { } export class OnoffApp { start(computers:Computer[], port:number = 3489) { ... } } export default function() { return new OnoffApp(); } } 

打字稿抱怨:

 service/example.ts(5,11): error TS2349: Cannot invoke an expression whose type lacks a call signature. service/example.ts(7,16): error TS2503: Cannot find namespace 'Onoff'. service/example.ts(8,21): error TS2503: Cannot find namespace 'Onoff'. typings/express/express.d.ts(14,1): error TS6053: File 'typings/serve-static/serve-static.d.ts' not found. typings/express/express.d.ts(28,34): error TS2307: Cannot find module 'serve-static'. 

非常感谢你! 我不知道我做错了什么

  1. 从错误描述来看,移动你的模块外部的import应该可以解决你的问题:

     import * as express from "express"; export module Onoff { ... } 
  2. 你导入你的模块作为onoff但正在尝试使用Onoff

     var onoff = require("./onoff"); ^ var computers: Onoff.Computer[] = []; ^