最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript duplicated type declarations - Stack Overflow

programmeradmin3浏览0评论

I'm experiencing issues with duplicated Typescript type declarations in the following case:

I've got the following dependency tree for my application A:

A->@angular/http:2.3.1
A->B->@angular/http:2.3.1

Both A and B are managed by npm. After running

npm install

the filesystem looks like this:

A/node_modules/
  @angular/http
  ...
  B/node_modules
     @angular/http

The problem seems to be that now there are two type declarations of @angular/http types like Response, or Headers. And somehow the Typescript transpiler seems unable to handle that - resulting in the following error message:

TS2453:The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. Type argument candidate 'Response' is not a valid type argument because it is not a supertype of candidate 'Response'. Types of property 'headers' are inpatible. Type 'Headers' is not assignable to type 'Headers'. Types have separate declarations of a private property 'mayBeSetNormalizedName'.

Reading the message, I guess this is a hickup of Typescript not being able to match the duplicated type declarations.

Anybody experienced the same issue? How to handle that problem? How to handle such name collisions?

I'm experiencing issues with duplicated Typescript type declarations in the following case:

I've got the following dependency tree for my application A:

A->@angular/http:2.3.1
A->B->@angular/http:2.3.1

Both A and B are managed by npm. After running

npm install

the filesystem looks like this:

A/node_modules/
  @angular/http
  ...
  B/node_modules
     @angular/http

The problem seems to be that now there are two type declarations of @angular/http types like Response, or Headers. And somehow the Typescript transpiler seems unable to handle that - resulting in the following error message:

TS2453:The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. Type argument candidate 'Response' is not a valid type argument because it is not a supertype of candidate 'Response'. Types of property 'headers' are inpatible. Type 'Headers' is not assignable to type 'Headers'. Types have separate declarations of a private property 'mayBeSetNormalizedName'.

Reading the message, I guess this is a hickup of Typescript not being able to match the duplicated type declarations.

Anybody experienced the same issue? How to handle that problem? How to handle such name collisions?

Share Improve this question edited Mar 20, 2017 at 17:29 Robert asked Mar 20, 2017 at 17:11 RobertRobert 1,9502 gold badges21 silver badges39 bronze badges 1
  • I know there is this related question: stackoverflow./questions/42412938/…. The answer's not really satisfactory, because in my case I cannot (do not want to) simply move the sources from B into A. There must be another way. – Robert Commented Mar 20, 2017 at 17:13
Add a ment  | 

2 Answers 2

Reset to default 4

Meanwhile I found out that you can fix this error by explicitly importing the according types inside the using class of A. In my case (cp. error message above), I needed to:

import {Response, Headers} from '@angular/http';

I had the same problem. There are basically two ways to solve this.

  1. Make a UMD module of project B. This might take a lot of time
  2. use as any as TheRequiredObject see below.

Let assume you got this class in project b:

export class B{
    getSome(): Observable {
        return this.http.get('some_url');
    }
}

and this is what you want in project a:

export class A{
    getSomeFromB: Observable{
        return B.getSome() as any as Observable;
    }
}
发布评论

评论列表(0)

  1. 暂无评论