Question

I recently started switching form PHP to Node.js, and since I'm a huge Typescript fan, I'm using Typescript + Node.js. I had my sample compiling just fine when I started to scale up and really build my code. But then I ran into an issues. Whenever node.d.ts is referenced (with the reference doc comment) in one of my .ts files, the Typescript compiler in Node.js complains about duplicated definitions. Two of my .ts files complain about not having the node.d.ts definitons, but my main.js file doesn't. (Files below:)

search_request.ts

/// <reference path="definitions/mustache.d.ts" />

import url = module("url");
import mu = module("mu2");

export function handler(request, response) {
    //code..
}

main.ts

/// <reference path="servers/search_request.ts" />

import search_request = module("./servers/search_request");
import express = module("express");

var app = express();

app.get("/search.html", search_request.handler);
app.listen(3000);

If I add <reference path="node.d.ts" /> to the top of search_request.ts, it compiles fine. If I remove it, I get warnings about missing definitions. However, if I include it in either file, compiling main.ts will give me hundreds of warnings about duplicated identifiers.

I'm not new to Typescript, but I'm new to Node.js and new to using the tsc compiler directly rather than through VS2012. What exactly am I doing wrong? Does the compiler implicitly include node.d.ts like lib.d.ts? And if so, why do I get errors when compiling search_request.ts?

Was it helpful?

Solution

So the change by Ryan Cavanaugh did fix my issue, but in a roundabout way. My real issue was exactly as you would expect: node.d.ts was included more than once (as was express.d.ts). My file structure was something like this:

C:\Project
   -node.d.ts
   \public
      -main.ts
      \definitions
         -node.d.ts

So naturally, in my main.ts file, I included definitions/node.d.ts. But somehow, node (or probably tsc) was automatically including the node.d.ts file that was one directly higher than main.ts. I don't know how, and it still confuses the hell out of me, but that was the issue.

OTHER TIPS

/// <reference path="servers/search_request.ts" />

import search_request = module("./servers/search_request");
import express = module("express");

A good rule of thumb is to never mix reference tags to non-.d.ts files with top-level import or export.

This should just work if you remove the reference tag to search_request.ts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top