缺少类型属性(打字稿)
Missing properties for type (typescript)
在以下内容中:
@Mutation
remove_bought_products(productsToBeRemoved: Array<I.Product>) {
const tmpProductsInVendingMachine: Array<I.Product> = Object.values(this.productsInVendingMachine);
const reducedProductsInVendingMachine: Array<I.Product> =
tmpProductsInVendingMachine.reduce((tmpProductsInVendingMachine, { id, ...rest }) => ({ ...tmpProductsInVendingMachine, ...{ [id]: { id, ...rest } } }), {});
productsToBeRemoved.forEach(({ id }) => reducedProductsInVendingMachine[id].productQty--);
...
给出:
TS2740: Type '{}' is missing the following properties from type 'Product[]': length, pop,
push, concat, and 28 more.
250 |
251 | const tmpProductsInVendingMachine: Array<I.Product> = Object.values(this.productsInVendingMachine);
> 252 | const reducedProductsInVendingMachine: Array<I.Product> =
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
253 | tmpProductsInVendingMachine.reduce((tmpProductsInVendingMachine, { id, ...rest }) => ({ ...tmpProductsInVendingMachine, ...{ [id]: { id, ...rest } } }), {});
254 | productsToBeRemoved.forEach(({ id }) => reducedProductsInVendingMachine[id].productQty--);
减速机是什么型号return?
产品是需要根据其 ID 建立索引的对象;例如
[{
id: 1,
productName: "coke",
productPrice: 2,
productQty: 20
},
...
]
reducedProductsInVendingMachine
不是 Array
,而是 Object
。
一种可能是在 Array.prototype.reduce()
的初始化参数中将 {}
转换为正确的类型:
const reducedProductsInVendingMachine =
tmpProductsInVendingMachine.reduce(
(tmpProductsInVendingMachine, { id, ...rest }) =>
({ ...tmpProductsInVendingMachine, ...{ [id]: { id, ...rest } } }),
{} as { [key: I.Product['id']]: I.Product }
);
注意实现是如何编译的,并且 reducedProductsInVendingMachine
变量类型被正确地推断为 { [key: I.Product['id']]: I.Product }
(I.Product['id']
解析为它是什么)
在以下内容中:
@Mutation
remove_bought_products(productsToBeRemoved: Array<I.Product>) {
const tmpProductsInVendingMachine: Array<I.Product> = Object.values(this.productsInVendingMachine);
const reducedProductsInVendingMachine: Array<I.Product> =
tmpProductsInVendingMachine.reduce((tmpProductsInVendingMachine, { id, ...rest }) => ({ ...tmpProductsInVendingMachine, ...{ [id]: { id, ...rest } } }), {});
productsToBeRemoved.forEach(({ id }) => reducedProductsInVendingMachine[id].productQty--);
...
给出:
TS2740: Type '{}' is missing the following properties from type 'Product[]': length, pop,
push, concat, and 28 more.
250 |
251 | const tmpProductsInVendingMachine: Array<I.Product> = Object.values(this.productsInVendingMachine);
> 252 | const reducedProductsInVendingMachine: Array<I.Product> =
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
253 | tmpProductsInVendingMachine.reduce((tmpProductsInVendingMachine, { id, ...rest }) => ({ ...tmpProductsInVendingMachine, ...{ [id]: { id, ...rest } } }), {});
254 | productsToBeRemoved.forEach(({ id }) => reducedProductsInVendingMachine[id].productQty--);
减速机是什么型号return?
产品是需要根据其 ID 建立索引的对象;例如
[{
id: 1,
productName: "coke",
productPrice: 2,
productQty: 20
},
...
]
reducedProductsInVendingMachine
不是 Array
,而是 Object
。
一种可能是在 Array.prototype.reduce()
的初始化参数中将 {}
转换为正确的类型:
const reducedProductsInVendingMachine =
tmpProductsInVendingMachine.reduce(
(tmpProductsInVendingMachine, { id, ...rest }) =>
({ ...tmpProductsInVendingMachine, ...{ [id]: { id, ...rest } } }),
{} as { [key: I.Product['id']]: I.Product }
);
注意实现是如何编译的,并且 reducedProductsInVendingMachine
变量类型被正确地推断为 { [key: I.Product['id']]: I.Product }
(I.Product['id']
解析为它是什么)