2015-11-19 82 views
2

什麼是必須的模塊代碼(以打字稿writen)看起來希望能夠利用這樣的: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
  1. 從錯誤的描述來看,移動import外面你的模塊應該解決您的問題:

    import * as express from "express"; 
    export module Onoff { 
        ... 
    } 
    
  2. 您導入模塊作爲onoff但試圖用Onoff

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

謝謝,固定日在onoff.ts錯誤,但不是其他錯誤。我相應地修改了這個問題。 – Databyte

+0

你導入你的模塊作爲'onoff',但正在嘗試使用'Onoff' –

+0

如果我這樣說,說'錯誤TS2305:模塊''服務/ onoff''沒有導出成員'計算機'..' – Databyte