Bødlen Bødlen - 5 months ago 10
Node.js Question

How to use DefinitetlyTyped definitions in Express app

How do i add Types from the Express definition to a function?

I have a function that takes an Express Request, and an Express Response as arguments, but i cannot get the type set correctly.

My standard JS function looks like this:

/// <reference path="../node.d.ts" />
/// <reference path="../express.d.ts" />

function postNewGame(req, res) {


But i'd like to enhance it with types from the Express Definition. Which should look something like this:

/// <reference path="../node.d.ts" />
/// <reference path="../express.d.ts" />

function postNewGame(req: Request, res: Response) {


But i what is the correct namespace or syntax to get the types to work?

The above sample gives this error
Could not find symbol 'Request'

Any advice/help would be appreciated.

Update 1:

I should note that the file where i'm trying to do this, is not the app.js where i import the express module. My function resides inside a
module that i
ins my app.js, and that might be my problem.

Update 2:

I made a simpler sample app to test this, and i'm still getting
Could not find symbol 'Request'
in my games.ts file.

My project structure is like this:

  • .ts-definitions

    • express.d.ts

    • node.d.ts

  • routes

    • games.ts

  • app.ts

My app.ts looks like this (super simplified):

/// <reference path=".ts-definitions/express.d.ts" />
var express = require('express')
var app = express();


http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));

and my games.ts like this:

/// <reference path="../.ts-definitions/express.d.ts" />

// How do i access express.Request, when express is not defined in the file?

module.exports = function (app) {
app.get('/games', function (req: express.Request, res: express.Response) { // express is not defined

To me it seems like the definitions only work in the file where i have
import express = require('express')


You can use import express = require('express'); even in files where you only use the interface/types. When you compile the file, if you don't actually use express, the TypeScript compiler is smart enough to leave it out of the compiled code.

/// <reference path="../.ts-definitions/express.d.ts" />

import express = require('express');

module.exports = function (app) {
  app.get('/games', function (req: express.Request, res: express.Response) {