【问题标题】:type 'MonoTypeOperatorFunction<any[]>' is not assignable to parameter of type 'OperatorFunction<Message, any[]>'MonoTypeOperatorFunction<any[]>' 类型不能分配给 'OperatorFunction<Message, any[]> 类型的参数
【发布时间】:2019-06-21 16:55:55
【问题描述】:

我正在尝试做一个剑道聊天示例:https://www.telerik.com/kendo-angular-ui/components/conversationalui/ 我使用 Visual Studio 代码和 Angular 6,我在谷歌搜索但找不到解决方案 但是我收到了这个错误:

src/app/app.component.ts(68,7) 中的错误:错误 TS2345:参数 类型“MonoTypeOperatorFunction”不可分配给参数 'OperatorFunction' 类型。参数类型 “来源”和“来源”不兼容。 类型“Observable”不可分配给类型“Observable”。 类型“消息”不可分配给类型“任何 []”。 “消息”类型中缺少属性“长度”。

我的组件是它:

import { Component } from '@angular/core';    
import { Subject } from 'rxjs/Subject';
import { switchMap } from 'rxjs/operators/switchMap';
import { map } from 'rxjs/operators/map';
import { windowCount } from 'rxjs/operators/windowCount';
import { scan } from 'rxjs/operators/scan';
import { take } from 'rxjs/operators/take';
import { tap } from 'rxjs/operators/tap';
import { from } from 'rxjs/observable/from';
import { merge } from 'rxjs/observable/merge';

import { ChatModule, Message, User, Action, ExecuteActionEvent, SendMessageEvent } from '@progress/kendo-angular-conversational-ui';
import { Observable } from 'rxjs/Observable';
import { ChatService } from './chat.service';

@Component({
  providers: [ ChatService ],
  selector: 'my-app',
  template: `
      <kendo-chat
        [messages]="feed | async"
        [user]="user"
        (sendMessage)="sendMessage($event)"
      >
      </kendo-chat>
    `
})
export class AppComponent {
  public feed: Observable<Message[]>;

  public readonly user: User = {
    id: 1
  };

  public readonly bot: User = {
    id: 0
  };

  private local: Subject<Message> = new Subject<Message>();

  constructor(private svc: ChatService) {
    const hello: Message = {
      author: this.bot,
      suggestedActions: [{
        type: 'reply',
        value: 'Neat!'
      }, {
        type: 'reply',
        value: 'Thanks, but this is boring.'
      }],
      timestamp: new Date(),
      text: 'Hello, this is a demo bot. I don\t do much, but I can count symbols!'
    };

    // Merge local and remote messages into a single stream
    this.feed = merge(
      from([ hello ]),
      this.local,
      this.svc.responses.pipe(
        map((response): Message => ({
          author: this.bot,
          text: response
        })
      ))
    ).pipe(
      // ... and emit an array of all messages
      scan((acc, x) => [...acc, x], [])
    );
  }

  public sendMessage(e: SendMessageEvent): void {
    this.local.next(e.message);

    this.local.next({
      author: this.bot,
      typing: true
    });

    this.svc.submit(e.message.text);
  }
}

我得到错误的行代码是:

scan((acc, x) => [...acc, x], [])

【问题讨论】:

  • 我在使用rxjs@^5.5 w/typescript@^3.1.1时遇到了类似的问题。在同一行出现错误:Argument of type 'UnaryFunction&lt;Observable&lt;any[]&gt;, Observable&lt;any[]&gt;&gt;' is not assignable to parameter of type 'UnaryFunction&lt;Observable&lt;Message&gt;, Observable&lt;any[]&gt;&gt;'
  • 我通过将 private local: Subject&lt;Message&gt; = new Subject&lt;Message&gt;(); 更改为 private local: Subject&lt;any&gt; = new Subject&lt;any&gt;(); 来修复我的问题

标签: angular rxjs angular6


【解决方案1】:

我也遇到了具体问题。尝试以下修复:

scan((acc:any, x) => acc.concat(x), [])

另请参阅:https://github.com/ReactiveX/rxjs/pull/2897#issuecomment-334654459

【讨论】:

    猜你喜欢
    • 2019-01-09
    • 2021-07-16
    • 2021-12-03
    • 2019-11-02
    • 2018-03-19
    • 2020-03-19
    • 2021-04-22
    • 1970-01-01
    • 2021-06-09
    相关资源
    最近更新 更多