Specifying type of function parameters with TypeScript
Problem
I'm trying to specify a function parameter type in a Node app. This is what I came up with:
import express = require("express");
module.exports = (app) => {
app.get('/', (req: express.Request, res: express.Response) => { //
and it generates this JS, which is not quite right, apparently:
define(["require", "exports"], function(require, exports) {
module.exports = function (app) {
//......
};
});
TS definitions are coming from DefinitelyTyped. "express" is defined as
declare module "express" {
in a .d.ts file.
Obviously i'm doing it wrong.. Any hints?
EDIT: To expand on basarat's answer: In csproj file find TypeScriptModuleKind entry, and set it to COMMONJS.
The ts ended up looking like this:
import express = require("express");
module.exports = (app: express.Application) => {
app.get("/", (req, res) => {
res.send({ name: "woohooo" });
});
}
Solution
The javascript generated looks like :
define(["require", "exports"], function(require, exports) {
module.exports = function (app) {
//......
};
});
because you are compiling with --module amd
. For node you should use --module commonjs
see : http://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1
PS: you need to reference the express type definitions for TypeScript to know about express usage : https://github.com/borisyankov/DefinitelyTyped/blob/master/express/express-tests.ts#L77 The definition : https://github.com/borisyankov/DefinitelyTyped/blob/master/express/express.d.ts
Discussion
View additional discussion.