pitosalas
pitosalas

Reputation: 10882

Relationship between import, require and /// <reference

I am new to TypeScript. Here's a snippet:

/// <reference path="typings/node/node.d.ts" />
const
    fs = require('fs'),
    spawn = require('child_process').spawn,
    filename = process.argv[2];

Why is the /// line required in this case? I thought that it would only be required with an import.

On the other hand, here's another snippet:

/// <reference path="typings/tsd.d.ts" />

import fs = require('fs');
import should = require('should');
var parse = require('csv-parse');

In this case, 'csv-parse' does not have a tsd file installed (in typings/) nor listed in tsd.json and yet that line does not give an error?

Upvotes: 0

Views: 132

Answers (1)

basarat
basarat

Reputation: 275927

Why is the /// line required in this case?

Don't recomment using reference comments anymore. These were before there was something like tsconfig.json : https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md These were to tell the IDE which other files need to be a part of the compilation context.

'csv-parse' does not have a tsd file installed (in typings/) nor listed in tsd.json and yet that line does not give an error?

This is because you are using var/require in var parse = require('csv-parse'); instead of import/require.

If you use import/require you will get an error. require is a function defined in node.d.ts which means var/require is allowed but not typechecked the same way as import/require.

Upvotes: 2

Related Questions