Argument of type 'ConnectionBackend' is no

2019-09-18 21:13发布

问题:

I have a question trying to figuring out what the issue here. I have created a custom Http class, ExtendHttp, which extends the Angular2 Http class and packaged it as a npm module. It compiles and test cases run within the project, but when I import it into other project using the npm module, it will give an error saying:

severity: 'Error' message: 'Argument of type 'ConnectionBackend' is not assignable to parameter of type 'ConnectionBackend'. Types of property 'createConnection' are incompatible. Type '(request: any) => Connection' is not assignable to type '(request: any) => Connection'. Two different types with this name exist, but they are unrelated. Type 'Connection' is not assignable to type 'Connection'. Two different types with this name exist, but they are unrelated. Types of property 'request' are incompatible. Type 'Request' is not assignable to type 'Request'. Two different types with this name exist, but they are unrelated. Types of property 'headers' are incompatible. Type 'Headers' is not assignable to type 'Headers'. Two different types with this name exist, but they are unrelated. Types have separate declarations of a private property 'mayBeSetNormalizedName'.' at: '35,42' source: 'ts'

This is my custom Http Class:

@Injectable()
export class ExtendedHttp extends Http {
    private logger: Logger;
    constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions,
                private config: ApplicationConfiguration,
                private evtsrv: EventService) {
        super(_backend, _defaultOptions);
        this.logger = new Logger(config.getLoggerConfig());
    }
...
}

This is how I use it in my other project:

@NgModule()
export class CoreModule {
    static forRoot(authzrule: AuthzRule[]): ModuleWithProviders {
    return {
        ngModule: CoreModule,
        providers: [
        { provide: UserSessionStorageService, useClass: UserSessionStorageService },
        {
        provide: Http,
        useFactory: (xhrBackend: ConnectionBackend, requestOptions: RequestOptions,
            config: ApplicationConfiguration, evtservice: EventService) =>
            new ExtendedHttp(xhrBackend, requestOptions, config, evtservice),
        deps: [ConnectionBackend, RequestOptions, ApplicationConfiguration, EventService]
},

回答1:

After realizing writing the CoreModule on the project where I import the npm module is a bad idea, hence moving the writing of CoreModule back to originate project and package it together with the npm module to resolve the issue.

Thanks Günter Zöchbauer for time & effort, you make realize where I have gone wrong.