簡體   English   中英

Typescript 中的 Node.js 模塊

[英]Node.js Module in Typescript

模塊代碼(用打字稿編寫)必須是什么樣子才能像這樣使用它:

/// <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[] = []; ^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM