Error in Angular-CLI: The return type of a public method from an exported class is referencing the name 'ErrorObservable' from an external module, but it cannot be named as such

Upon completing the development of an app that mirrors an existing Angular 2 (non-cli) application, I am encountering errors in several of my files now that the project has been transitioned to Angular-CLI. I am puzzled as to why these errors are arising in the CLI version but were absent in the previous iteration. Could this be a consequence of Webpack integration? Or perhaps a difference in how Typescript is handled between the two builds? It's worth noting that the original app utilized SystemJS while the Angular-CLI version employs Webpack. By "building out" the app, I mean that I meticulously recreated all the components, directives, modules, and routes from the original app in this new version. The common error message that recurs across multiple files is as follows:

Error: Return type of public method from exported class has or is using name 'ErrorObservable' from external module but cannot be named

Here's an excerpt from a file that triggers this error:

import { Observable } from 'rxjs/Observable';
import { Http, Response } from '@angular/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

@Injectable()
export class StreamCountsService {

    private _url: string = "https://api.somesite.com"

    constructor(private _http: Http) {}

    getCount() {
        return this._http.get(this._url)
            .map((response:Response) => response.json())
            .catch(this._errorsHandler);
    }
    _errorsHandler(error: Response) {
        console.error(error);
        return Observable.throw(error || "Server Error");
    }
}

My Environment: MacBook Air, OS X El Capitan

Angular-CLI Version Currently using Angular-CLI version 1.0.0-beta.22-1

The complete error trace is as follows (slightly redacted for privacy reasons):

Error details...

Below are the dependencies listed in my package.json file:

{
  "name": "cli-ark",
  "version": "0.0.1",
  "license": "MIT",
  ...
  detailed package.json contents here
  ...
}

Answer №1

Here is a helpful tip for handling errors:

return Observable.throw(error.json().error || 'Server error');

If you are using a version of rxjs greater than rc.5, do this:

import { ErrorObservable } from 'rxjs/observable/ErrorObservable';

Instead of:

import 'rxjs/add/observable/throw';

Remember, these lines are unnecessary:

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

Answer №2

When upgrading to version 5 or above of rx, it is recommended to switch to

ErrorObservable

instead of using

throw

Your code needs to be updated as follows:

import { Observable } from 'rxjs/Observable'; 
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
...  
this.somethingReturnObservable.get()
  .filter( val => val*2)
  .do((val) => {
    // do something for example
  })
  .switchMap(trans => Observable.throw( 'some error' ))
  .catch( (er, c) => dosomething);

To:

import { ErrorObservable } from 'rxjs/observable/errorObservable';
import { switchMap, tap, catchError, filter } from 'rxjs/operators';
...
this.somethingReturnObservable.get()
  .pipe(
    filter( val => val*2)
    tap( (val) => {// do something for example} ),
    switchMap( trans => ErrorObservable.create( 'some error' ) ),
    catchError( (er, c) => dosomething);
  );

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for initiating printing in a separate window?

Is there a way to modify the code below so that when I click "Print" it opens in a new window instead of redirecting and losing the original receipt? <div class="print_img"> <button onclick="myFunction()"> <div align="justify ...

Learn how to merge two objects and return the resulting object using TypeScript functions within the Palantir platform

I am looking to generate a pivot table by combining data from two objects using TypeScript functions. My plan is to first join the two objects, create a unified object, and then perform groupBy operations along with aggregate functions like sum and min on ...

How to display the name of the parent object using JavaScript's prototypal inheritance

I've been delving into JavaScript prototypal inheritance and the prototype property, and I decided to create a fiddle to gain a deeper understanding. However, I'm struggling to comprehend why my example isn't functioning as expected. In the ...

Having trouble with the Angular 2 QuickStart setup on your Linux system?

Currently in the process of setting up the Angular 2 Quick Start Tutorial from scratch on a virtualbox Linux machine build. Followed all steps as outlined in the 5-minute quick start, however, encountering an error when running "npm start": /media/sf_test ...

Do not begin the next task until the current function has properly concluded

I am currently developing a project in Ionic v4 with Angular that involves using BLE to communicate with a Raspberry Pi. The project consists of several steps: Searching for devices around me Connecting to the desired device Activating notifications Sen ...

Discover instances of a string within an array using JQuery

I am currently exploring how to locate occurrences of a specific string within an array on the client side. The examples provided on the JQuery Docs all seem focused on number comparisons, which isn't quite what I need. Essentially, I'm attempti ...

The module 'DynamicTestModule' experienced an import of an unexpected value under the name 'SohoComponentsModule'

I have been working on an angular 2 application using our UI team's library. The progress has been smooth, thanks to the easy integration of their components through import statements in my app.module.ts: import { BrowserModule } from '@angular/ ...

Trouble with launching Jasmine tests within define block in compiled Typescript

Currently, I am immersed in a testing project that involves setting up a pure Javascript Jasmine Karma environment to test a pre-compiled Typescript setup. Despite my efforts, I am facing an issue where the test cases refuse to start running. While the co ...

Guide to creating a Chrome extension that can detect updates in MongoDB documents

I have developed a chrome extension for syncing links, which stores the data in a MongoDB database using a custom REST API. While I am successfully able to push changes to the database and listen to them using a change stream in the REST API, I am facing d ...

Switch between hiding and showing the DIV element flaw

I am currently working on a piece of code that involves simple HTML and JS to show/hide elements. Here is the code snippet: // Enabling forEach for 'querySelectorAll' and 'getElementsByClassName' HTMLCollection.prototype.forEach = No ...

Ajax handling all tasks except for adding HTML elements

Having an issue with my basic "Load More on Scroll" AJAX function. The console is showing that the HTML is being sent back from the request, but for some reason, nothing is being rendered on the page. I must be missing something really simple here. $(wi ...

Turn off client-side hydration in Nuxt.js or Prevent leaking raw data in Nuxt.js

Working on a Web App built with Nuxt.js for Server-Side Rendering poses some challenges. To safeguard my backend data, I turned to asyncData and Axios for communication with the server. However, Nuxt.js inadvertently exposed my backend data to clients th ...

Determine the width of two inline input fields

Showing two inputs side by side: +------------+ +--------------------------+ | ID='inputA'| | ID='inputB' | +------------+ +--------------------------+ +------------------------------------------+ A ...

Tips for resuming a video playback in HTML5 after pausing it for a brief moment

I am working with a video called introVid, and my goal is for it to pause for 2 seconds when it reaches the 1 second mark before resuming playback. Although I've attempted to achieve this using the code below, the video remains in a paused state after ...

"Exploring the world of Typescript declarations and the CommonJS module

I'm encountering confusion while creating declaration files (d.ts). For instance, I have developed an NPM package called "a" with a single CommonJS module index.ts: export interface IPoint { x: number; y: number; } export default function s ...

Connecting the SelectedItem of a listbox in ngPrime to an Observable Collection in Angular2

I am facing an issue while trying to use the ngPrime listbox in Angular2. I have a scenario where I am fetching an array of objects from Firebase as an observable and attempting to display it correctly in the listbox. <div *ngIf="addContactDialogVisibl ...

What are some ways to customize the functionality of the data table filter in Angular Material?

Attempting to use the filter feature in Angular Material Data Table: When searching for "MATCHED", both "MATCHED" and "UNMATCHED" are displayed in the status column of the table. It seems this is due to the data object being reduced and concatenated befo ...

Encountered an optimization error during the docker build process with Ng build

Encountering an error while trying to build an Angular app in docker, specifically when running npm run build: #11 1.106 > ng build #11 1.106 #11 4.769 - Generating browser application bundles (phase: setup)... #11 32.23 ✔ Browser application bundle g ...

Is it possible to generate code snippets for TypeScript in Visual Studio 2015?

Is it possible to create a code snippet for Angular 1.x directives written in TypeScript, even though there may be some product level configuration required? I have come across similar questions discussing TypeScript snippets on Visual Studio, but the res ...

Unable to locate the name 'Cheerio' in the @types/enzyme/index.d.t file

When I try to run my Node application, I encounter the following error: C:/Me/MyApp/node_modules/@types/enzyme/index.d.ts (351,15): Cannot find name 'Cheerio'. I found a suggestion in a forum that recommends using cheerio instead of Cheerio. H ...