angular - 在“从不”类型上不存在
问题描述
今天,我尝试写一个reducer.ts并得到了我定义initialstate如下的错误
import { ActionsUnion, ActionTypes } from './actions';
export const initialState = {
items: [] ,
cart: []
};
当我在下面得到错误时
case ActionTypes.Remove:
return {
...state,
cart: [...state.cart.filter(item => item.name !== action.payload.name)]
};
它声明 item.name 属性 'name' 在 item.name 中的类型 'never'.ts(2339) 上不存在,我知道我应该为 initalstate 创建接口但我不知道该怎么做。任何人都可以建议吗?
谢谢
解决方案
您可以为所有必要的接口创建一个文件夹,如下所示:
src/interfaces/item.interface.ts
export interface Item {
name: string;
id: number; // an example
description: string; // an example
}
src/interfaces/cart.interface.ts
export interface Cart {
// same here, add the necessary properties
}
然后,在你的initialState
import { ActionsUnion, ActionTypes } from './actions';
import { Item } from 'src/interfaces/item';
import { Cart } from 'src/interfaces/cart';
export const State = {
items: Item[],
cart: Cart[]
};
export const initialState: State = {
items: [ {
name: ''
}],
cart: []
}
推荐阅读
- r - 如何计算定义为类对象“字符”的向量中的数字?
- docker - 运行整个 docker 容器,并将卷安装在内存中
- c# - 如何让 Service Fabric 反向代理在 Azure 上工作
- amazon-web-services - 如何将文件从 Google Cloud Instance 传输到 AWS Instance
- c - 为什么用 0 初始化数组会清除整个缓冲区?
- c# - 如何匹配字符串忽略大小写和重音?
- c# - 如果它为“null”,是否可以使用设置的 TargetNullValue 更新数据绑定源值?
- visual-studio - 有没有办法远程调试部署到虚拟目录的 ASP.net 应用程序?
- cordova - 错误:找不到automationName“Appium”和platformName“Android”的驱动程序
- python - 使用 keras 的图像分类预测不同类别的相同输出