简体   繁体   English

打字稿重复类型声明

[英]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类型的类型声明,如ResponseHeaders 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. 基本上有两种方法可以解决这个问题。

  1. Make a UMD module of project B. This might take a lot of time 制作项目B的UMD模块。这可能需要很长时间
  2. use 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.

相关问题 Parcel 不会为 TypeScript 库生成类型声明 - Parcel does not generate type declarations for a TypeScript library 将 Typescript 类型声明添加到 Monaco 编辑器 - Adding Typescript Type Declarations to Monaco Editor @typescript-eslint/no-unused-vars 类型声明中的误报 - @typescript-eslint/no-unused-vars false positive in type declarations 如何使用 React 和 TypeScript 将实现与类型声明分开? - How can I separate a implementations from type declarations with React and TypeScript? JavaScript模块的TypeScript声明 - TypeScript declarations for JavaScript module Typescript 动态类型声明 - Typescript dynamic types declarations 后续变量声明必须有相同的类型错误 lodash require TypeScript - Subsequent variable declarations must have the same type error for lodash require TypeScript VueJS/Typescript - 找不到模块“./components/Navigation”或其相应的类型声明 - VueJS/Typescript - Cannot find module './components/Navigation' or its corresponding type declarations Angular新手:调试打字稿错误“'imageLoader'的所有声明必须具有相同的类型参数” - Angular Newbie: Debugging Typescript Error “All declarations of 'imageLoader' must have identical type parameters” ReScript,TypeScript:找不到模块“@rescript/react/src/ReactDOM.gen”或其相应的类型声明 - ReScript, TypeScript: Cannot find module '@rescript/react/src/ReactDOM.gen' or its corresponding type declarations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM