The correct way to reference modules from other modules in Typescript

I am writing a module for NodeJS in Typescript. I am trying to process a request (which should be an IncomingMessage object) using this module.

/// <reference path="typings/node/node.d.ts"/> module rateLimiter { export function processRequest(req : http.IncomingMessage) : Boolean { return false; }; } 

When trying to make sure that the parameter of the incoming req request is such an instance, I found that I could not refer to anything from the http module. I think to myself "good, so I need to import it because it's just an alias." However, when I do this, I get: "Imported queries in the namespace cannot reference the module."

 /// <reference path="typings/node/node.d.ts"/> module rateLimiter { import http = require('http');//IMPORT DECLARATIONS IN A NAMESPACE CANNOT REFERENCE A MODULE export function processRequest(req : http.IncomingMessage) : Boolean { return false; }; } 

So, I try, what seems like a bad decision, importing globally, only to get "it is impossible to compile modules if the flag flag is not specified"

 /// <reference path="typings/node/node.d.ts"/> import http = require('http');//CANNOT COMPILE MODULES UNLESS --MODULE FLAG IS PROVIDED module rateLimiter { export function processRequest(req : http.IncomingMessage) : Boolean { return false; }; } 

I feel that I am fundamentally lacking in how such a link should be made. It seems that I did not need to import the module in order to use the definitions included in node.d.ts. Can someone shed some light on this?

+3
typescript
Jun 22 '15 at 19:30
source share
1 answer

If you write a module, nothing is written to the global scope - the file itself is a module, and everything inside it is tied to this module.

 import http = require('http'); export function processRequest(req : http.IncomingMessage) : boolean { return false; }; 

In the above example, the rateLimiter.ts file is a module. http imported into the rateLimiter module.

You need to compile the module flag - for example:

 tsc --module commonjs rateLimiter.ts 

Most editors and IDEs provide a way to install this too.

+1
Jun 22 '15 at 19:49
source share



All Articles