typescript “void”类型的参数不能分配给“Action”类型的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40733712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:01:34  来源:igfitidea点击:

Argument of type 'void' is not assignable to parameter of type 'Action'

typescriptvisual-studio-code

提问by Khushi

i'am new to Typescript and using VSCode.

我是 Typescript 的新手并使用 VSCode。

Getting following Error:

得到以下错误:

*[ts] Argument of type 'void' is not assignable to parameter of type 'Action'. (parameter) action: void

*[ts]“void”类型的参数不能分配给“Action”类型的参数。(参数)动作:void

Code:

代码:

  loadItems() {
    return this.Apiname.find()
        .map(
        (data) => console.log("data:", data)
        )
        .map(
        payload => ({
            type: 'LOAD_ITEMS',
            payload: payload
        },
        )
        )
        .subscribe(
        action => this._store.dispatch(action)//Error
        );
};

Any help will be very helpfull.

任何帮助都会非常有帮助。

采纳答案by Radim K?hler

Your mapstatement contains trailing ,, which could be the problem. I'd adjust the code like that:

您的map语句包含 trailing ,,这可能是问题所在。我会像这样调整代码:

return this.Apiname.find()
    .do( (data) => console.log("data:", data) )
    .map(
        payload => ({
            type: 'LOAD_ITEMS',
            payload: payload
        //},)
        })
    )
    .subscribe(
        action => this._store.dispatch(action) // no error
    );

now actionis of type { type: string, payload: ... }

现在action是类型{ type: string, payload: ... }

NOTE: for side effects, like printing with console.log, ReactiveX provides .dooperator - see http://reactivex.io/documentation/operators/do.html

注意:对于副作用,比如用 打印console.log,ReactiveX 提供 .do操作符 - 请参阅http://reactivex.io/documentation/operators/do.html