Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript compiler errors when including node.d.ts

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?

like image 202
GJK Avatar asked Apr 01 '13 23:04

GJK


People also ask

Can you run TypeScript with node?

You can run typescript directly on the node with the ts-node package. This package is recommended for development only. To make the final deploy in production, always use the javascript version of your project. The ts-node is already included as a dependency on another package, t ts-node-dev .

Does NPM work with ts?

via npm. You can use npm to install TypeScript globally, this means that you can use the tsc command anywhere in your terminal. To do this, run npm install -g typescript . This will install the latest version (currently 4.8).

What is ts-node TypeScript?

ts-node is a TypeScript execution engine and REPL for Node. js. It JIT transforms TypeScript into JavaScript, enabling you to directly execute TypeScript on Node. js without precompiling. This is accomplished by hooking node's module loading APIs, enabling it to be used seamlessly alongside other Node.

Does TypeScript have a compiler?

The TypeScript compiler, named tsc , is written in TypeScript. As a result, it can be compiled into regular JavaScript and can then be executed in any JavaScript engine (e.g. a browser). The compiler package comes bundled with a script host that can execute the compiler.


2 Answers

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.

like image 164
GJK Avatar answered Sep 24 '22 13:09

GJK


/// <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.

like image 31
Ryan Cavanaugh Avatar answered Sep 26 '22 13:09

Ryan Cavanaugh