简体   繁体   English

错误 TS2345:“事件”类型的参数不可分配给“IPokemon”类型的参数

[英]error TS2345: Argument of type 'Event' is not assignable to parameter of type 'IPokemon'

Running this code:运行这段代码:

 <app-pokemon-form *ngIf="showPokemonForm" (newPokemonEvent)="addNewPokemon($event)" 
    (pokemonFormClose)="pokemonFormClose($event)"
      [pokemon]="currentPokemon"></app-pokemon-form>

Causes an error:导致错误:

error TS2345: Argument of type 'Event' is not assignable to parameter of type 'IPokemon'.错误 TS2345:“事件”类型的参数不可分配给“IPokemon”类型的参数。

Here is the TypeScript File:这是 TypeScript 文件:

 addNewPokemon(newPokemon: IPokemon): void {
    console.log('adding new pokemon ' + JSON.stringify(newPokemon));
    this.pokemonService.addPokemon({ ...newPokemon })
      .subscribe({
        next: pokemon => {
          console.log(JSON.stringify(pokemon) + ' has been added');
          this.message = "new pokemon has been added";
        },
        error: (err) => this.message = err
      });

And here is the pokemon model itself:这是口袋妖怪 model 本身:

export interface IPokemon {
    _id: string,
    Name: string,
    Generation: Number,
    Type: string
}

Any Help Would Be Greatly Appreciated任何帮助将不胜感激

Since your argument $event is of type Event , meaning it does not (necessarily) have the form of由于您的参数$eventEvent类型,这意味着它(不一定)具有以下形式

interface IPokemon {
  _id: string,
  Name: string,
  Generation: number,
  Type: string
}

you cannot pass $event to your addPokemon method.您不能将$event传递给您的addPokemon方法。

You can check the form of the $event parameter if it matches with the IPokemon interface and if您可以检查$event参数的形式是否与IPokemon接口匹配,如果

  • not : make sure that $event has the appropriate form, or not :确保$event具有适当的形式,或者
  • if yes: you can add $event as unknown as IPokemon in your function call如果是:您可以在 function 通话中添加$event as unknown as IPokemon

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TS2345:“事件”类型的参数不可分配给“HtmlInputEvent”类型的参数 - TS2345: Argument of type 'Event' is not assignable to parameter of type 'HtmlInputEvent' 错误 TS2345:“事件”类型的参数不可分配给“{目标:{值:字符串; }; }' - error TS2345: Argument of type 'Event' is not assignable to parameter of type '{ target: { value: string; }; }' Angular2 / TypeScript:错误TS2345:类型'String'的参数不能分配给'string'类型的参数 - Angular2/TypeScript: error TS2345: Argument of type 'String' is not assignable to parameter of type 'string' Ngrx/effects:获取错误 TS2345:“Product[]”类型的参数不可分配给“Product”类型的参数 - Ngrx/effects: get error TS2345: Argument of type 'Product[]' is not assignable to parameter of type 'Product' 错误TS2345:类型为&#39;(a:Test,b:Test)=&gt;布尔| 1&#39;不能分配给类型&#39;(a:Test,b:Test)=&gt; number&#39;的参数 - error TS2345: Argument of type '(a: Test, b: Test) => boolean | 1' is not assignable to parameter of type '(a: Test, b: Test) => number' TS2345:“日期”类型的参数 | null' 不可分配给类型为“string |”的参数日期 | 不明确的' - TS2345: Argument of type 'Date | null' is not assignable to parameter of type 'string | Date | undefined' TS2345:“可观察”类型的参数<todointerface[]> ' 不能分配给 'TodoInterface[]' 类型的参数</todointerface[]> - TS2345: Argument of type 'Observable<TodoInterface[]>' is not assignable to parameter of type 'TodoInterface[]' 人员类型的 TypeScript 错误 (TS2345) 参数 | undefined 不能分配给 Person 类型的参数 - TypeScript Error (TS2345) argument of type Person | undefined is not assignable to paramater of type Person TS 错误:“null”类型的参数不可分配给“错误”类型的参数 | 承诺喜欢<error | undefined> | 未定义'.ts(2345)</error> - TS error: Argument of type 'null' is not assignable to parameter of type 'Error | PromiseLike<Error | undefined> | undefined'.ts(2345) typescript:类型&#39;any []&#39;的参数不能分配给&#39;[]&#39;类型的参数.ts(2345) - typescript : Argument of type 'any[]' is not assignable to parameter of type '[]'.ts(2345)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM