简体   繁体   English

{} 类型的参数不能分配给 [] 类型的参数

[英]Argument of type {} is not assignable to parameter of type []

I working in angular 2 at the moment, I get error after mapping data from backend, and try to call function.我目前在 angular 2 工作,从后端映射数据后出现错误,并尝试调用 function。 Error text below下面的错误文字

Argument of type '{ date: Date; '{ date: Date; 类型的参数recipes: Recipe[];食谱:食谱[]; }[]' is not assignable to parameter of type 'Day[]'. }[]' 不可分配给“Day[]”类型的参数。

The Day model looks like this:天 model 看起来像这样:

import { Recipe } from "../recipes/recipe.model";

export class Day {
public date: Date;
public recipes :Recipe[]

constructor(date: Date, recipes ?: Recipe[]) {
    this.date = date;
    this.recipes = recipes;
  }

getDate(){
    return this.date;
  }
}

Error occurred in setDays method, the passed parameter is invalid. setDays 方法出错,传入的参数无效。

loadDays() {
    return this.http.get<Day[]>(
        this.urlDays,
    ).pipe(
        map((days => {
            return days.map(day => {
                return {
                    ...day,
                    recipes: day.recipes ? day.recipes : []
                };

            });
        })),
        tap(days => {
            this.daysService.setDays(days);
        }),
    );
}

In this part of code:在这部分代码中:

return {
                    ...day,
                    recipes: day.recipes ? day.recipes : []
                };

I check if data i get have recipes if they have i don't chenge recipe, and if i don't have recipe i add empty array of type Recipe.我检查我得到的数据是否有食谱,如果他们有我没有食谱,如果我没有食谱,我添加食谱类型的空数组。

DaysService code:天服务代码:

import { Injectable } from "@angular/core";
import { Subject } from "rxjs";

import { Day } from "./day.model";

@Injectable()
export class DaysService {

  private startDate: Date = new Date(new Date().getTime());
  private dayLongInMS: number = 86400000;
  private daysShown: Array<Day> = [];
  daysChanged = new Subject<Day[]>();

  getDays() {
        return this.daysShown.slice();
  }

  setDays(days: Day[]) {
        this.daysShown = days;
        this.daysChanged.next(this.daysShown.slice());
    }

I think i need some method to grouped back this 2 parameters after divided it be operator triple dot (date: Date; recipes: Recipe[];) to one (Day[]) property.我想我需要一些方法来将这两个参数划分为运算符三点(日期:日期;食谱:食谱 [];)到一个(天 [])属性。

I find similar problem here: Argument of type {...} is not assignable to parameter of type [ ] But I don't know how to use it me case.我在这里发现了类似的问题: Argument of type {...} is not assignable to parameter of type [ ]但我不知道如何使用它。 Can you give me some advice?你能给我一些建议吗?

You are not returning instances of the Day class.您不会返回Day class 的实例。 The http client doesn't create Day instances just because they're given as the generic type. http 客户端不会创建Day实例,因为它们是作为泛型类型给出的。 And the compiler is complaining because you are creating objects that don't conform to the interface of the Day class.编译器在抱怨,因为您正在创建不符合Day class 接口的对象。

Instead, you will need to create Day instances in the map operator.相反,您需要在map运算符中创建Day实例。

loadDays() {
  return this.http.get(this.urlDays).pipe(
    map((response: any) => {
      return response.map(day => new Day(day.date, day.recipes || []));
    }),
    tap(days => {
      this.daysService.setDays(days);
    }),
  );
}

If you want to ensure that Day instances are always initialised with a non-null recipes , you should handle that in your constructor rather than relying on the calling code.如果您想确保Day实例始终使用非空recipes进行初始化,您应该在构造函数中处理它,而不是依赖调用代码。

export class Day {
  public date: Date;
  public recipes :Recipe[]

  constructor(date: Date, recipes ?: Recipe[]) {
    this.date = date;
    this.recipes = recipes || [];
  }

  getDate(){
    return this.date;
  }
}

暂无
暂无

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

相关问题 “X”类型的参数不能分配给“Y”类型的参数 - Argument of type 'X' is not assignable to parameter of type 'Y' 'Date[]' 类型的参数不可分配给'number[]' 类型的参数 - Argument of type 'Date[]' is not assignable to parameter of type 'number[]' 令人困惑的编译器错误:“字符串”类型的参数不可分配给类型参数 - Confusing compiler error: Argument of type 'string' is not assignable to parameter of type “any”类型的参数不可分配给“never”类型的参数 typescript 解决方案 - argument of type 'any' is not assignable to parameter of type 'never' typescript solution Typescript object const 断言键入 -.includes() “'string' 类型的参数不可分配给 'never' 类型的参数。” - Typescript object const assertion typing - .includes() “Argument of type 'string' is not assignable to parameter of type 'never'.” * 类型的参数与 * 类型的参数不兼容 - argument of type * is incompatible with parameter of type * 错误:参数类型int与参数类型不兼容 - ERROR: argument type int incompatible for parameter type Vue 存储数组声明中的“不能分配给从不类型的参数”TS 错误 - "Not assignable to parameter of type never" TS error in Vue store array declaration 类型 &#39;number&#39; 不能分配给类型 className [] - Type 'number' is not assignable to type className [] 打字稿:类型 &#39;Promise&lt;{}&gt;&#39; 不可分配给类型 - Typescript: Type 'Promise<{}>' is not assignable to type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM