1

Looking at the documentation in section Working with Other JavaScript Libraries, I would have expected to see two imports:

  1. The first for the .d.ts file
  2. The second for the actual implementation of the javascript file.

However it appears that only the .d.ts is being imported, by a weird comment reference and then URL is loaded which is declared inside the .d.ts file.

/// <reference path="node.d.ts"/>
import * as URL from "url";
let myUrl = URL.parse("http://www.typescriptlang.org");

So how does this actually work? Perhaps I might have expected to see a reference from the .d.ts file to the implementation, but I don't see that either. Is there some magic involved?

Edit: Further request for clarification.

Please confirm that the import * line referenced above does not load anything directly from the .d.ts file. It's confusing given that the first line of the file is:

declare module "url" {

which suggests that this is where the module is being loaded from. I had thought that the reference line was used to indicate the path to indicate the .d.ts file and then the import line was loading the type information from this file.

The fact that some .d.ts files declare entities with var suggested to me that perhaps more than simply type information was contained within.

1 Answer 1

1

One of typescript main purposes is to provide stronger type system that lacks in javascript. That is what d.ts files are for - they describe the "content" of the javascript libraries so that your typescript transpiler or IDE can provide you with handy typechecks and so on. When you compile your typescript application all type information is lost and you have pure javascript as an output.

In regards to your question - you reference node.d.ts in order to give transpiler/IDE a hint what url, path, fs, etc. modules are in node.js - so you get type checking. The importing of the actual url module itself has nothing to do with typescript and is done by node.js in accordance with its module resolution rules

EDIT

Declaration files does not result in any code emitted by typescript compiler (link). Therefore it is impossible to load (import) anything from them by the nature of their definition.

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

2 Comments

Thank you for your answer. I've expanded to the question to request for further clarification of my misunderstanding. If you could possibly address this I can then mark your answer as correct.
I have added a link and a small clarification - hope this helps.

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.