Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RxJS with TypeScript

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!

like image 861
ba0708 Avatar asked Apr 29 '16 08:04

ba0708


People also ask

Is RxJS a TypeScript?

It's interesting to know that the RxJS project is using TypeScript actively and it helped to find bugs when the library was migrating from JavaScript.

Can we use RxJS in JavaScript?

RxJS is a JavaScript library that uses observables to work with reactive programming that deals with asynchronous data calls, callbacks and event-based programs. RxJS can be used with other JavaScript libraries and frameworks. It is supported by JavaScript and also with typescript.

Can we use RxJS in Angular?

Reactive Extensions for JavaScript, or RxJS, is a JavaScript library that uses observables for reactive programming. It can be used with other JavaScript libraries and frameworks, and it integrates well into Angular.

What is an Observable in TypeScript?

An Observable is a collection of multiple input values that get processed using array methods such as map , reduce , filter , and so on. It comes in handy when handling asynchronous operations such as making HTTP requests, user-input events, and so on.


2 Answers

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.

like image 118
OJ Kwon Avatar answered Sep 19 '22 20:09

OJ Kwon


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.

like image 40
Latin Warrior Avatar answered Sep 21 '22 20:09

Latin Warrior