3

I followed this tutorial (really straightforward): https://www.techiediaries.com/rxjs-tutorial/

My index.ts looks like this:

import { Observable, Observer } from "rxjs";
import { map } from "rxjs/operators"

console.log(Observable);

var observable = Observable.create((observer:Observer<string>) => {
    observer.next('Hello');
    observer.next('Hello Again');
    observer.complete();
});

observable.map((s:string) => s).subscribe((x:any) => {
    console.log(x);
});

webpack-dev-server starts without problem. It seems that "Observable.create" also works.

My problem: not a single operator will work. I get the errror when I open the page:

Uncaught TypeError: observable.map is not a function

I am searching for 2 hours now - every article explains that this should be the correct way to use RxJs 6 with TypeScript.

My tsconfig.json:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "es6",
    "moduleResolution": "node",
    "target": "es6",
    "allowJs": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

My webpack.config.js:

const path = require('path');
module.exports = {
    entry: './src/index.ts',
    devtool: 'inline-source-map',
    module: {
      rules: [
        {
          test: /\.tsx?$/,
          use: 'ts-loader',
          exclude: /node_modules/
        }
      ]
    },
    resolve: {
      extensions: [ '.tsx', '.ts', '.js' ]
    },
    output: {
      filename: 'bundle.js',
      path: path.resolve(__dirname, 'dist')
    }
};

1 Answer 1

4

Because your're using the old syntax for operators. The current one consists in using pipeable operators:

import { Observable, Observer } from "rxjs";
import { map } from "rxjs/operators"

console.log(Observable);

var observable = Observable.create((observer:Observer<string>) => {
    observer.next('Hello');
    observer.next('Hello Again');
    observer.complete();
});

observable.pipe(
  map(s => s.toLowercase())
).subscribe(s => console.log(s));

Note: your map didn't do anything useful, so I made it do something. Using any is really a bad idea, so I removed that too, in addition to using type inference.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.