7

My understanding of typescript definition files is that they are used to provide tooling support and are not required to compile typescript.

However given the following:

app.ts

import {Observable} from 'rx';

Observable
    .interval(1000)
    .subscribe(n => console.log(n));

Running:

npm install typescript rx --save
.\node_modules\.bin\tsc app.ts --module commonjs 

Gives the error:

app.ts(1,26): error TS2307: Cannot find module 'rx'.

Import the type definitions for rx fixes this

app.ts

/// <reference path="./node_modules/rx/ts/rx.all.d.ts" />

import {Observable} from 'rx';

Observable
    .interval(1000)
    .subscribe(n => console.log(n));

Questions

  • It appears that the definition files are required, is this always the case?
  • The rx npm package includes the definition files. Can typescript automatically search the node modules folder to find them without me having to explicitly reference them?

Update

basarat was correct about the typings property. See Typings for npm packages for more information

kimsagro
  • 15,513
  • 17
  • 54
  • 69

1 Answers1

5

It appears that the definition files are required, is this always the case?

No. If the module does it properly using the typings property it would just work. More: https://basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html

The rx npm package includes the definition files. Can typescript automatically search the node modules folder to find them without me having to explicitly reference them?

Only if they have typings setup properly ... which they don't.

mixel
  • 25,177
  • 13
  • 126
  • 165
basarat
  • 261,912
  • 58
  • 460
  • 511
  • Hmmm.. this does not seem to be the case with angular2. I may be mistaken, but the angular2 npm package does not require you to import the type definition however it's package.json file does not have the typings property. Am I missing something? – kimsagro Dec 09 '15 at 22:08
  • If you do `'angular2'` it will not work. However they ship with `angular2.d.ts` in the *root* of the package hence `angular2/angular2` *does work* – basarat Dec 10 '15 at 01:01
  • 1
    I still don't understand why typings are required. What if a package doesn't include its typings and there's no existing definition anywhere? I thought types in TypeScript were optional but it seems you cannot use a package without its typings... – empz Jun 16 '16 at 02:42
  • @basarat This is also requires `"moduleResolution": "node"` typescript compiler option. – mixel Jul 07 '16 at 13:22
  • @emzero, have you got your answer since? – Harshit Juneja Jun 01 '19 at 15:45