5

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?

1
  • I have the same problem but I don't have the file double ... Commented Jun 20, 2013 at 20:19

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Same thing had me chasing my tail. Using the typings package to install the node typings gave me a "main" and a "browser" directory with a node.d.ts in each. I excluded the browser one as I wasn't using it and viola!
2
/// <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.

3 Comments

Thank you, that didn't solve all of my problems, but it solved one of them. :p There's a LOT more quirks with Node.js Typescript vs. browser Typescript.
It turns out your answer did fix an issue, but wasn't the root cause of the problem. You can see my answer to see what was really going on.
I actually had a similar error but the root cause was that our project depends on an old version of node (0.12). Most (if not all) of the *.d.ts files out there reference the generic node.d.ts which defaults to the current version of 6.x. The fix for me was to rename the node-0.12.d.ts to node.d.ts and update my tsconfig.json to reference this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.