[英]Typescript duplicated type declarations
I'm experiencing issues with duplicated Typescript type declarations in the following case: 在以下情况下,我遇到了重复的Typescript类型声明的问题:
I've got the following dependency tree for my application A: 我的应用程序A有以下依赖树:
A->@angular/http:2.3.1
A->B->@angular/http:2.3.1
Both A and B are managed by npm. A和B都由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 . 问题似乎是现在有两种@ angular / http类型的类型声明,如Response或Headers 。 And somehow the Typescript transpiler seems unable to handle that - resulting in the following error message:
并且不知何故,Typescript转换器似乎无法处理 - 导致以下错误消息:
TS2453:The type argument for type parameter 'T' cannot be inferred from the usage.
TS2453:无法根据用法推断类型参数“T”的类型参数。 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 incompatible.
属性“标题”的类型不兼容。 Type 'Headers' is not assignable to type 'Headers'.
“标题”类型不能指定为“标题”类型。 Types have separate declarations of a private property 'mayBeSetNormalizedName'.
类型具有私有属性'mayBeSetNormalizedName'的单独声明。
Reading the message, I guess this is a hickup of Typescript not being able to match the duplicated type declarations. 阅读消息,我想这是一个不能匹配重复类型声明的Typescript的hickup。
Anybody experienced the same issue? 有谁遇到过同样的问题? How to handle that problem?
如何处理这个问题? How to handle such name collisions?
如何处理这样的名称冲突?
同时我发现你可以通过在A的using类中显式导入相应的类型来修复这个错误。在我的情况下(上面的cp。错误信息),我需要:
import {Response, Headers} from '@angular/http';
I had the same problem. 我有同样的问题。 There are basically two ways to solve this.
基本上有两种方法可以解决这个问题。
as any as TheRequiredObject
see below. as any as TheRequiredObject
使用as any as TheRequiredObject
见下文。 Let assume you got this class in project b: 假设你在项目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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.