TypeScript, Node.js, require and module in Visual Studio 2013

Is it incorrect to use this code in a TypeScript file in Visual Studio 2013 for a Node.js code base?

// toolset_1.ts, this file is referenced in other .ts files
declare function require(name: string);
declare var module;

declare var __dirname;

Setup:

  • Project is created using node in a directory.
  • A TypeScript project created in the same directory, using VS 2013.

Current Situation/Configuration:

  • Me: C# developer, Node.js and TypeScript noob.

  • TypeScript: Module system option is set to None.

  • Project Description: A simple play ground with Node.js, Express.js and bunch of other tools with some TypeScript code.

  • Project Status: It works (Simple GET(Hogan.js), POST, PUT and DELETE request are being servered)

For example this is pile.ts in routes directory (express):

/// <reference path="./lib/toolset_1.ts" />

var express = require('express');
var router = express.Router();

router.get('/', function (req, res, next) {
    res.render('pile', { title: 'PILED', msg: '[PILED]' });
});

router.post('/', function (req, res) {
    res.send({ time: new Date(), msg: 'post [PILED]' });
});

router.put('/', function (req, res) {
    res.send({ time: new Date(), msg: 'put [PILED]' });
});

router.delete('/', function (req, res) {
    res.send({ time: new Date(), msg: 'delete [PILED]' });
});

module.exports = router; 

Is it incorrect to use this code in a TypeScript file in Visual Studio 2013 for a Node.js code base?

It is okay. But I would do something else:

  • get the file node.d.ts from definitely typed. Also get others like express.d.ts
  • reference the file using /// <reference
  • compile with commonjs : --module commonjs
  • Use import/require instead of var/require:

i.e.

import express = require('express');

More on external modules : https://www.youtube.com/watch?v=KDrWLMUY0R0