简体   繁体   English

类型 'string' 不能分配给类型 '{ 模型:{ 节点:[]; 链接:[]; }; }'.ts(2322)

[英]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.

相关问题 类型“元素”不可分配给类型“字符串”.ts(2322) - Type 'Element' is not assignable to type 'string'.ts(2322) 类型 'HTMLButtonElement' 不可分配给类型 'string'.ts(2322) - Type 'HTMLButtonElement' is not assignable to type 'string'.ts(2322) 类型 &#39;string&#39; 不可分配给类型 &#39;(url: string) =&gt; string&#39;.ts(2322) - Type 'string' is not assignable to type '(url: string) => string'.ts(2322) 键入'字符串 | 号码 | boolean' 不能分配给类型 'undefined'。 类型“字符串”不可分配给类型“未定义”.ts(2322) - Type 'string | number | boolean' is not assignable to type 'undefined'. Type 'string' is not assignable to type 'undefined'.ts(2322) 类型“未知”不可分配给类型“从不”.ts(2322) - Type 'unknown' is not assignable to type 'never'.ts(2322) 类型 &#39;undefined&#39; 不能分配给类型 &#39;number&#39; .ts(2322) - Type 'undefined' is not assignable to type 'number' .ts(2322) 类型“编号”不可分配给类型“PersonConfig”。 ts(2322) - Type 'number' is not assignable to type 'PersonConfig'. ts(2322) angular2 TS2322类型&#39;Promise <void> &#39;不可分配给&#39;Promise <string> &#39;当给定字符串时 - angular2 TS2322 Type 'Promise<void>' is not assignable to type 'Promise<string>' when given a string TypeScript - TS2322:类型“string”不可分配给类型“str1”| “str2”&#39; - TypeScript - TS2322: Type 'string' is not assignable to type '“str1” | “str2”' React TypeScript 错误:TS2322 - 类型''不可分配给类型'IntrinsicAttributes &amp; String' - React TypeScript Error : TS2322 - Type '' is not assignable to type 'IntrinsicAttributes & String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM