17

I have a problem trying to use RxJS with TypeScript and Node.js. I am using NPM, and I have included rxjs-es version 5. I am also using Typings, and I have included both es6-shim and rx.all, like so:

{
  "ambientDependencies": {
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654",
    "rx.all": "registry:dt/rx.all#2.2.28+20160316155526"
  }
}

Below is my tsconfig.json file.

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/browser",
    "typings/browser.d.ts"
  ]
}

In a TypeScript file, I am trying to do the following:

import { Observable } from 'rxjs/Observable';

When I try to compile the script, I get the following error:

typings/main/ambient/rx.all/index.d.ts(10,11): error TS2304: Cannot find name 'Rx'.

This happens even if I try to use RxJS within my script or not, so the problem is related to the TypeScript typings.

What do I need to install using typings and NPM in order to use RxJS? In particular, I am interested in using Observables. After Googling for hours, I cannot seem to figure out just what I need to make it work. I have tried the following, but without any luck. I have also tried many combinations of rx packages in NPM and typings, but haven't found anything that works. Any help is much appreciated!

2
  • 1
    Do you have an example of TypeScript + RxJs without Angular? I have a the same error: node_modules/rxjs/Observable.d.ts(10,66): error TS2304: Cannot find name 'Promise' and I haven´t been able to fix it.... With just this import and no more core: import { Observable } from 'rxjs/Observable'; Commented Sep 21, 2016 at 20:34
  • How do you compile your TS source? import { Observable } from 'rxjs/Rx' should just work. Commented Jan 8, 2018 at 18:11

6 Answers 6

18

RxJS 5 is written in typescript and its type definitions are included by default for published module, you may not need typings for rx.all. Actually those type definitions are written for previous versions of RxJS. Simply uninstall rx.all type definition and try to import without those.

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

1 Comment

but by default the types definations are not available. do you have any sample project without angularjs or other library
14

The correct way of accomplishing this for Angular (not AngularJS) is by using @types from npm. The command is as follows:

npm install @types/rx --save-dev

For more information, see npm.

Comments

4

Generally, you don't need to install typings separately, just including rxjs/Rx in your project should work (note the package name):

// installed with `$ npm i rxjs`
import { Observable } from 'rxjs/Rx' // <- rxjs from NPM

Observable
    .of('Hello')
    .map( w => w + ' world!') // Annotated properly: (w: string) => string
    .subscribe(console.log.bind(console)) // 'Hello world!'

My .tsconfig (node)

Shouldn't be relevant, but feel free to use/compare.

{
    "compilerOptions": {
        "target": "es2015",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "removeComments": false,
        "noImplicitReturns": true,
        "noImplicitAny": true,
        "preserveConstEnums": true
    },
    "exclude": [
        "logs",
        "node_modules"
    ]
}

Comments

1

That typings file depends on Rx:

declare module "rx.all" {
    export = Rx;
}

Try installing "rx" as well as "rx.all":

typings install rx --save --ambient

But notice these typings assume you are importing "rx.all" or "rx" so your import will not give you typing info:

import { Observable } from 'rxjs/Observable';

Can you import from "rx" instead of "rxjs"?

import { Observable } from 'rx';

Comments

1

You can install typings for RXJS by entering the following in your CLI.

typings install --global dt~es6-shim --save

This will add the following to your typings.json "es6-shim": "registry:dt/es6-shim#0.31.2+20160602141504"

Then add a reference path to the top of your typescript file

/// <reference path="typings/globals/es6-shim/index.d.ts" />`

or to your tsconfig.json.

This will remove any typescript errors.

Comments

-3

If you are using TypeScript 2 you can try this.

typings install npm:rx/ts/rx.all.d.ts --save --global

1 Comment

isn't it @types now instead of typings...? confusing :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.