Typescript errors setting ad to TRUE

I am trying to create typescript definitions for an Angular 2 project that I am working on so that it can be an exported library.

I have several services that return HTTP requests to components, everything is very similar to the following:

public create(user:User) { return this.http.post(this._apiUrls.create, JSON.stringify(user), { headers: this.apiConfig.getApiHeaders() }); } 

That I then call something like this from the component:

 Session.create(user).subscribe((res:Response) => { this.user = res.json().user }); 

All this works fine until I return the 'declaration' to true in the tsconfig file so that I can create typescript definition files. I am starting to get the following errors for several of my services:

 error TS4053: Return type of public method from exported class has or is using name 'Observable' from external module "node_modules/rxjs/Observable" but cannot be named. 

I basically understand the problem, but I do not know the solution. If I import an Observable into a service, then the typescript linter will cause errors, because technically it is not used in this file.

Starting with Angular 1, it was a similar paradigm that we used in all our applications to break our code, but maybe I need to change the approach in Angular 2? I looked through many other Angular 2 examples, and they all did it in a similar way.

+5
source share
1 answer

To date, the compiler will not automatically import types for you into the declaration file.

The best workaround at the moment is to manually disable the lint rules for importing or importing the type and using explicit type annotation so that it indicates it as use.

In other words

 // Explicit import of 'Observable' so that '.d.ts' files // can be generated correctly. import { Observable } from "node_modules/rxjs/Observable"; // Explicit use of 'Observable' to satisfy your linter. public create(user: User): Observable { return this.http.post(this._apiUrls.create, JSON.stringify(user), { headers: this.apiConfig.getApiHeaders() }); } 
+5
source

Source: https://habr.com/ru/post/1242268/


All Articles