【发布时间】:2020-04-16 06:54:37
【问题描述】:
我有一个名为 Products 的界面
export interface Products{
category: string;
imageUrl: string;
price: number;
title: string;
}
我的产品数组类型的组件中有一个变量
products: Products[];
我正在尝试将我的响应从我的服务映射到 products 变量,但我收到此错误类型
'{}[]' is not assignable to type 'Products[]'
我不知道我做错了什么
this.subscription = this.productService
.getAll()
.subscribe(
products =>
(this.products = products.map(p => ({ ...(p.payload.val() as {}) }))),
)
【问题讨论】:
-
你的方法
p是做什么的? (地图功能内) -
对不起,这是一个错误。应该是 p => ({...p.payload.val() as {}}
-
有没有一个叫做
payload的东西有一个方法?我见过的大多数有效载荷都是纯数据对象。 -
@gbubemismith
p指的是产品吗? -
你在里面明确地做了一个
as {};如果类型可推断,您是要执行as Product还是放弃as子句?您基本上是在告诉编译器生成{}[]。
标签: javascript typescript firebase-realtime-database angular8