类型"ConnectionBackend"的参数不能分配给类型"ConnectionBackend"的参数 [英] Argument of type 'ConnectionBackend' is not assignable to parameter of type 'ConnectionBackend'

查看:29
本文介绍了类型"ConnectionBackend"的参数不能分配给类型"ConnectionBackend"的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题试图找出这里的问题.我创建了一个自定义Http类ExtendHttp,该类扩展了Angular2 Http类并将其打包为npm模块.它可以编译并测试在项目中运行的用例,但是当我使用npm模块将其导入其他项目时,它将给出错误消息:

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:

严重性:'错误'消息:'类型为'ConnectionBackend'的参数为不可分配给"ConnectionBackend"类型的参数.种类属性"createConnection"不兼容.类型'(request:any)=>连接'不能分配给类型'(request:any)=> Connection'.两种不同的名称存在,但它们无关.不能将连接"类型分配给连接"类型.存在两种使用此名称的不同类型,但是它们是无关的.属性"request"的类型不兼容.请求"类型不能分配给请求"类型.存在两种使用此名称的不同类型,但是它们是无关的.属性标题"的类型不兼容.不能将标题"类型分配给标题"类型.存在两种使用此名称的不同类型,但是它们是无关的.类型具有专用属性'mayBeSetNormalizedName'的单独声明.来源:"35,42",来源:"ts"

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'

这是我自定义的Http类:

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]
},

推荐答案

意识到在我导入npm模块的项目上编写了CoreModule后,这是一个坏主意,因此将CoreModule的写法移回原始项目并打包到一起使用npm模块来解决该问题.

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.

感谢GünterZöchbauer的时间&努力,您就会意识到我出了问题.

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

这篇关于类型"ConnectionBackend"的参数不能分配给类型"ConnectionBackend"的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆