'可观察的<T>'不是从'Observable&lt;T&gt;'派生的类 [英] &#39;Observable&lt;T&gt;&#39; is not a class derived from &#39;Observable&lt;T&gt;&#39;

查看:45
本文介绍了'可观察的<T>'不是从'Observable&lt;T&gt;'派生的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图从 node_modules 中的类扩展一个类时,打字稿编译器会抛出一个错误说:

When trying to extend a class from a class in a node_modules the typescript compiler throws a error saying:

属性 'source' 受保护,但类型为 'Observable'不是从'Observable'派生的类.

这仅在基类来自 node_module 时发生.

This only happens when the base class is from a node_module.

基类看起来像:

import {Observable} from "rxjs/Observable";
export abstract class TestBase<T> {

    request(options: any):Observable<T> {
        return Observable.throw(new Error('TestBase is abstract class. Extend it and implement own request method'));
    }
}

在项目中对其进行子类化:

Subclassing it in a project:

import {Observable} from "rxjs/Observable";
import {TestBase} from "@org/core";

class SocketResponse {

}

class Socket {
    request(): Observable<SocketResponse> {
        return new Observable.of(new SocketResponse());
    }
}

export class Sub extends TestBase<SocketResponse> {
    request(options:any):Observable<SocketResponse> {
        return new Socket().request();
    }
}

如果基类 (TestBase) 从 node_module 移动到它自己的项目并将导入更改为看起来像import {TestBase} from "./base"; 错误消失.

If the base class (TestBase) is moved from the node_module to the project it self and change the import to look like import {TestBase} from "./base"; The error disappears.

这是因为编译为每个模块创建了不同范围的类型吗?我完全迷失在这里.

Is this due to that the compiles creates the types in different scopes for each module? I'm completely lost here.

更新:

这似乎只有在将 node_modulesnpm link 链接时才会发生.目前似乎一种可能的解决方法是不返回基类中的类型以返回接口.

This seems to only happen when linking the node_modules with npm link. Seems like one possible workaround for the moment is to instead of returning a type in the base class to return a interface.

可在此处找到更多信息:

More information can be found here:

https://github.com/Microsoft/TypeScript/issues/6496

https://github.com/ReactiveX/rxjs/issues/1744

推荐答案

我刚刚在为项目开发自定义模块时遇到了一个非常相似的问题.我不能 100% 确定我们遇到了同样的问题,但看起来很接近.

I just came across a very similar issue while developing a custom module for a project. I'm not 100% sure we were having the same problem, but it looks pretty close.

我建议删除您的 node_modules 文件夹并重新安装所有依赖项.

I would suggest deleting your node_modules folder and reinstalling all your dependencies.

rm -rf node_modules/
npm cache clean
npm install

这为我解决了.

我正在开发的模块有一个主要项目试图扩展的抽象类.但是,当尝试编译主项目时,编译器会抛出与您相同的错误.

The module I was developing had an abstract class that the main project was trying to extend. However when trying to compile the main project, the compiler would throw the same error you are getting.

经过一番探索,我注意到 NPM 在将我的模块安装到我的项目中时会抱怨 UNMET PEER DEPENDENCY.在查看项目的 node_modules 内部时,我注意到我的模块有另一个 node_modules 文件夹嵌套在其中,其中包含一些与主项目共享的依赖项.

After a bit of digging around, I noticed NPM would complain about an UNMET PEER DEPENDENCY when installing my module into my project. When looking inside the node_modules of the project, I noticed that my module had another node_modules folder nested inside of it with some dependencies that it shared with the main project.

我不确定这一点,但我认为 NPM 认为该模块期望它与主项目共享不同版本的依赖项.

I'm not certain about this, but I think NPM thought the module was expecting different version of dependencies it shared with the main project.

因此,模块内的抽象类从其自己的嵌套 node_modules 文件夹中引用 Observable,而主项目从顶级 node_modules 文件夹中引用 Observable.

So the abstract class inside the module was referencing Observable from its own nested node_modules folder while the main project was referencing Observable from the top level node_modules folder.

在尝试解决我的问题时,其他问题为我提供了一些见解:

This other questions provided some insight for me when trying solve my problem:

为什么 npm install 说我有未满足的依赖项?

这篇关于'可观察的<T>'不是从'Observable&lt;T&gt;'派生的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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