【问题标题】:Recompose mapPropsToStream typescript重构 mapPropsToStream 打字稿
【发布时间】:2018-10-16 21:51:22
【问题描述】:

我正在尝试在 typescript 上编写组件,将 props 中的 onChange 替换为 rxjs 实现 onChange,如下所示

        this.onChangeSubscription = this.onChange$
        .debounceTime(200)
        .filter(value => value.length !== 1)
        .distinctUntilChanged()
        .subscribe(value => this.props.onChange(value))

当我尝试使用 pluck 函数时出现错误:

Subscribable 类型上不存在属性“pluck”

     const Filter = mapPropsStream<Props,Props>((props$) => {    
            const a = props$.pluck('onChange');
            // newProps created
            return newProps$;
        })(Component);

导入包含

import 'rxjs/add/operator/pluck';

【问题讨论】:

    标签: typescript rxjs recompose


    【解决方案1】:

    您正在使用 RxJS,但 recompose 不认为可观察的实现和 can be configured 与许多其他库一起使用。

    即使有了该配置,TypeScript 仍会将 props$ 视为非 RxJS 类型。为了解决这个问题,你可以使用特定的类型断言,或者你可以使用 RxJS from 函数:

    import { Observable } from 'rxjs/Observable';
    import 'rxjs/add/observable/from';
    
    const Filter = mapPropsStream<Props,Props>((props$) => {    
        const a = Observable.from(props$).pluck('onChange');
        // newProps created
        return newProps$;
    })(Component);
    

    【讨论】:

    • return props$.combineLatest如何相处?
    • 在你需要从props$ 获得一个 RxJS observable 时使用Observable.from,你从recompose 收到。
    • 这仍然会产生 tsc 错误:TS2345: Argument of type 'Subscribable&lt;{}&gt;' is not assignable to parameter of type 'ObservableInput&lt;ObservableInput&lt;{}&gt;&gt;'. Property 'then' is missing in type 'Subscribable&lt;{}&gt;' but required in type 'PromiseLike&lt;ObservableInput&lt;{}&gt;&gt;'
    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多