简体   繁体   English

键入'字符串 | 号码 | boolean' 不能分配给类型 'undefined'。 类型“字符串”不可分配给类型“未定义”.ts(2322)

[英]Type 'string | number | boolean' is not assignable to type 'undefined'. Type 'string' is not assignable to type 'undefined'.ts(2322)

I'm trying to create a partial object that only has certain fields of the full object that meet a criteria.我正在尝试创建一个部分 object,它只有满足标准的完整 object 的某些字段。 However, I get the subject typescript error message when I try to assign the property.但是,当我尝试分配属性时,我收到主题 typescript 错误消息。 I created a test module to illustrate the concept/problem.我创建了一个测试模块来说明概念/问题。 Note that this is only to illustrate the problem.请注意,这只是为了说明问题。 It is not the actual code.这不是实际的代码。

type FullObject = {
  id: number
  name: string
  active: boolean
}

type PartialObject = Partial<FullObject>

const myFullObj: FullObject = {
  id: 1,
  name: 'First Object',
  active: true,
}

const myPartialObj: PartialObject = {}

let k: keyof PartialObject

for (k in myFullObj) {
  if (myFullObj[k] !== undefined) myPartialObj[k] = myFullObj[k] // Error here
  if (k === 'name') myPartialObj[k] = myFullObj[k] // No error here
}

Note that it is only the first "if" statement that has the error.请注意,只有第一个“if”语句有错误。 After some research and trying various things, I worked around the problem by initializing the partial object to the full object and then deleting properties that did not meet a criteria.经过一些研究和尝试各种事情后,我通过将部分 object 初始化为完整的 object 然后删除不符合标准的属性来解决该问题。 Since this is a backwards way of solving the problem, I would prefer to create the partial object with properties that meet criteria.由于这是解决问题的一种倒退方式,我更愿意创建具有符合标准的属性的部分 object。

I came up with the following solution.我想出了以下解决方案。 It clearly illustrates what I am trying to do: if a criteria is met with a source object property, then copy that property into the partial destination object.它清楚地说明了我要做什么:如果源 object 属性满足条件,则将该属性复制到部分目标 object 中。 In this example I'm using "not undefined" as the criteria.在这个例子中,我使用“not undefined”作为标准。 In the real code the criteria is more complex.在实际代码中,标准更复杂。

type FullObject = {
  id: number
  name: string
  active: boolean
}

type PartialObject = Partial<FullObject>

const myFullObj: FullObject = {
  id: 1,
  name: 'First Object',
  active: true,
}

let myPartialObj: PartialObject = {}

let k: keyof PartialObject

for (k in myFullObj) {
  if (myFullObj[k] !== undefined) myPartialObj = { ...myPartialObj, ...Object.fromEntries([[k, myFullObj[k]]]) }
}
console.log(JSON.stringify(myPartialObj, null, '  '))

It seems that there must be a better way to accomplish this.似乎必须有更好的方法来实现这一点。 However, the example illustrates what is intended.但是,该示例说明了预期的目的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型 &#39;undefined&#39; 不能分配给类型 &#39;number&#39; .ts(2322) - Type 'undefined' is not assignable to type 'number' .ts(2322) TS2322:类型“属性”不可分配给类型“布尔值 | 不明确的'。 切换 - TS2322: Type 'Atrribute' is not assignable to type 'boolean | undefined'. Toggle 类型“元素”不可分配给类型“字符串”.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) 类型“字符串”不可分配给类型“未定义” - Type 'string' is not assignable to type ' undefined' 类型 &#39;string&#39; 不可分配给类型 &#39;(url: string) =&gt; string&#39;.ts(2322) - Type 'string' is not assignable to type '(url: string) => string'.ts(2322) 类型“编号”不可分配给类型“PersonConfig”。 ts(2322) - Type 'number' is not assignable to type 'PersonConfig'. ts(2322) 输入'字符串[] | undefined' 不可分配给类型 'string | 不明确的' - Type 'string[] | undefined' is not assignable to type 'string | undefined' 类型 &#39;string&#39; 不能分配给类型 &#39;{ 模型:{ 节点:[]; 链接:[]; }; }&#39;.ts(2322) - Type 'string' is not assignable to type '{ model: { nodes: []; links: []; }; }'.ts(2322) 参数类型编号不可分配给参数类型字符串 | 未定义类型编号不可分配给类型字符串 - Argument type number is not assignable to parameter type string | undefined Type number is not assignable to type string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM