javascript - Node.js modul v Typescripte


Ako musí vyzerať kód modulu (napísaný v TypeScripte), aby ho bolo možné používať takto:

/// <reference path="./onoff.ts" />

//import * as onoff from "./onoff";
var onoff = require("./onoff");

var app = onoff();

var computers: Onoff.Computer[] = [];
app.start(computers);

Bol som si istý, že to musí byť toto, ale nefunguje to:

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();
    }
}

Typescript si sťažuje:

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'.

Ďakujem veľmi pekne! Nemám ani tušenia, čo som urobil zle...

Odpoveď č. 1

  1. Podľa popisu chyby by presunutie import mimo váš modul malo vyriešiť váš problém:

    import * as express from "express";
    export module Onoff {
        ...
    }
    
  2. Importujete svoj modul ako onoff, ale snažíte sa použiť Onoff

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