Argument of type 'any' is not assignable to parameter of type 'never'.ts(2345)
Получаю ошибку, при попытке добавить объект CartItem в массив foods.
Argument of type 'any' is not assignable to parameter of type 'never'.ts(2345)
Я так понимаю, что проблема в том, что foods объявлен без указания типа.
foods: []
Как решить эту проблему?
export class CartService {
items$: CartItem[] = [];
constructor() {
this.items$ = [];
}
getItemsInCart() {
return this.items$;
}
}
export class CartItem {
quantity = 1;
food: any;
constructor(food: any) {
this.food = food;
}
}
export class CartComponent implements OnInit {
model = {
Name: '',
State: '',
foods: []
};
constructor(private cart: CartService) {
}
ngOnInit() {}
onSubmit() {
this.cart.getItemsInCart().forEach(cartItem => {
this.model.foods.push(cartItem.food);
});
}
}
Ответы (1 шт):
Автор решения: SwaD
→ Ссылка
Вам надо типизировать вашу переменную model
type ModelType = {
Name: string;
State: string
foods: any[];
};
const model: ModelType = {
Name: '',
State: '',
foods: [],
}
model.foods.push('строка');
model.foods.push(1); // число
model.foods.push([]);
model.foods.push({});