Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import RXJS types for TypeScript

If I understand correctly, rxjs (version 5) is written in typescript and is packaged with all the definitions.

I tried using them, but I cannot seem to find a way to do that. I get the error

error TS2304: Cannot find name 'Observable'

Here is my tsconfg.json

{
  "compilerOptions": {
    "target": "es2016",
    "strict": true
  },
  "exclude": [ "node_modules" ]
}

and the file I try to compile

const { Observable } = require("@reactivex/rxjs")

function timer(time: Number): Observable {
  return Observable.timer(time)
}

I run node_modules/.bin/tsc test.ts

Am I missing some typescript config here? Is there something to do to enable types?

like image 258
atomrc Avatar asked Jun 25 '17 14:06

atomrc


2 Answers

Can be due to the bug in version RxJS version 7.4.0. https://github.com/ReactiveX/rxjs/issues/6641

like image 177
Ievgen Avatar answered Oct 19 '22 17:10

Ievgen


The typescript definitions for Observable Subject etc from RXJS are bundled and installed with the RXJS package. So if you run npm install rxjs you will get the type description files (*.td) included in the npm module.

In this case I think your issue is with the import of the Observable module. It should read:

import { Observable } from 'rxjs/Observable';

like image 35
edzillion Avatar answered Oct 19 '22 18:10

edzillion