88869

Correct way to reference modules from other modules in Typescript

Question:

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

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

When attempting to ensure that the incoming request parameter req is such an instance, I find that I cannot reference anything from the http module. I think to myself "okay, so I need to import it because that's just an alias". When I do so however, I receive "import delcarations in a namespace cannot reference a module."

<pre class="lang-ts prettyprint-override">/// <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 poor decision, importing in the global scope, only to receive "cannot compile modules unless --module flag is provided"

<pre class="lang-ts prettyprint-override">/// <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 like I'm fundamentally missing how this sort of reference is supposed to be made. It feels like I shouldn't have to import a module just to use the definitions included in node.d.ts. Could someone shed some light on this?

Answer1:

If you are writing a module, nothing you write is in the global scope - the file itself is a module, and everything inside it is scoped to that module.

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

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

You need to compile with the module flag - for example:

tsc --module commonjs rateLimiter.ts

Most editors and IDEs supply a way to set this too.

Recommend

  • Can't access my folder because of WordPress [closed]
  • Open view controller when remote notification pressed
  • NodeJS using node-serialport sometimes becomes unresponsive
  • Redirect response to download file
  • Global session variable in express.js route?
  • parsing xml and html page with lxml and requests package in python
  • C++ Coin flip program error
  • string.IsNullOrEmpty() Doesn't Seem to Work on a String within a Class within a Class
  • Express JS Display Data By ID
  • Sending Content-Type application/x-www-form-urlencoded WSO2 ESB
  • SSIS Designer is running VERY slowly
  • CORS with socket.io
  • Activation Function choice for Neural network
  • Access user's phone number on iOS 7
  • WPF Template Binding in ToggleButton UserControl
  • Android Studio Can't Find tools.jar
  • UWP/C# - Issue with AQS and USB Devices
  • System.InvalidCastException: Specified cast is not valid
  • Unable to decode certificate at client new X509Certificate2()
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Ensure fsync did its job
  • PHP - How to update data to MySQL when click a radio button
  • Symfony2: How to get request parameter
  • ORA-29908: missing primary invocation for ancillary operator
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Turn off referential integrity in Derby? is it possible?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • JaxB to read class hierarchy
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?