[英]Type 'string' is not assignable to type '{ model: { nodes: []; links: []; }; }'.ts(2322)
I'm new on typescript, here i have added types to a project of mine, it is giving me error on one of types at graph: "" : Type 'string' is not assignable to type '{ model: { nodes: [];我是打字稿的新手,在这里我向我的一个项目添加了类型,它在图形中的一种类型上给了我错误:“”:类型“字符串”不可分配给类型“{模型:{节点:[ ]; links: [];
链接:[]; };
}; }'.ts(2322)
}'.ts(2322)
The expected type comes from property 'graph' which is declared here on type 'InitialGraphArticleState'
any help/suggestion is appreaciated.任何帮助/建议都得到了认可。
interface InitialGraphArticleState { graph: { model: { nodes: []; links: [] } }; currentNode: string; currentLink: string; currentCameraNode: string; currentCameraLinks: []; floorplan: string; } const initialGraphArticleState: InitialGraphArticleState = { graph: "", currentNode: "", currentLink: "", currentCameraNode: "", currentCameraLinks: [], floorplan: "", }; export function graphArticleReducer( state = initialGraphArticleState, action: graphArticleReducerAction ) { }
In your initialGraphArticleState
object, the property graph
does not match the interface definition: You are basically a assigning a string
to a property of type { model: { nodes: []; links: [] } }
在您的
initialGraphArticleState
对象中,属性graph
与接口定义不匹配:您基本上是将string
分配给类型为{ model: { nodes: []; links: [] } }
{ model: { nodes: []; links: [] } }
. { model: { nodes: []; links: [] } }
。
You can either change the object initialization as follow:您可以按如下方式更改对象初始化:
interface InitialGraphArticleState {
graph: { model: { nodes: [], links: [] } };
currentNode: string;
currentLink: string;
currentCameraNode: string;
currentCameraLinks: [];
floorplan: string;
}
const initialGraphArticleState: InitialGraphArticleState = {
graph: { model: { nodes: []; links: [] } },
currentNode: "",
currentLink: "",
currentCameraNode: "",
currentCameraLinks: [],
floorplan: "",
};
Or update your interface definition to allow a null
graph object and initialize to that instead:或者更新您的接口定义以允许
null
图形对象并初始化为该对象:
interface InitialGraphArticleState {
graph: { model: { nodes: [], links: [] } } | null;
currentNode: string;
currentLink: string;
currentCameraNode: string;
currentCameraLinks: [];
floorplan: string;
}
const initialGraphArticleState: InitialGraphArticleState = {
graph: null,
currentNode: "",
currentLink: "",
currentCameraNode: "",
currentCameraLinks: [],
floorplan: "",
};
Considering how you are using the graph
property in the reducer, I would advise the first solution, otherwise you need to check that the property is not null before trying to use its properties.考虑到您如何在 reducer 中使用
graph
属性,我会建议第一个解决方案,否则您需要在尝试使用其属性之前检查该属性是否为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.