TS18048: ” 可能是 ‘undefined’

huangapple go评论139阅读模式
英文:

TS18048: '' is possibly 'undefined'

问题

我正在使用TypeScript,但是我遇到了一个错误"TS18048:'record.childList1' 可能为 'undefined'。",我猜这是因为CheckListItemVO中的childList1是可选的。我尝试了以下两种写法:

  1. record.childList1[0]?.pciName
  2. (record.childList1[0] as CheckListItemChildVO).pciName

但仍然出现相同的错误。

这是我的代码:

<Table.Column
   key='childList1'
   dataIndex='childList1'
   title={<div style={{ textAlign: 'center' }}>standard</div>}
   render={(text, record: CheckListItemVOExtender) => (
      <div style={{ display: 'flex', flexDirection: 'column' }}>
      <p style={{ whiteSpace: 'pre-wrap' }}>{record.childList1[0]?.pciName}</p>              
      </div>)}
/>

type CheckListItemVOExtender = CheckListItemVO & {
  sparPoint: string | number;
};

export type CheckListItemVO = CCheckListItem & {
  childList1?: CheckListItemChildVO[]; 
}

export type CCheckListItem = {
  pciName?: string; 
}

大多数情况下,当我遇到这种错误时,我会使用可选标记或在末尾使用"as Type"(就像第二种方法一样),但在这种情况下不起作用。是否有人可以帮助?

英文:

I am using TypeScript, but I got an error "TS18048: 'record.childList1' is possibly 'undefined'.", which I guess it caused due to childList1 was optional in CheckListItemVO. I tried to write

  1. record.childList1[0]?.pciName
  2. (record.childList1[0] as CheckListItemChildVO).pciName

but sill get the same error.

Here is my code

&lt;Table.Column
   key=&#39;childList1&#39;
   dataIndex=&#39;childList1&#39;
   title={&lt;div style={{ textAlign: &#39;center&#39; }}&gt;standard&lt;/div&gt;}
   render={(text, record: CheckListItemVOExtender) =&gt; (
      &lt;div style={{ display: &#39;flex&#39;, flexDirection: &#39;column&#39; }}&gt;
      &lt;p style={{ whiteSpace: &#39;pre-wrap&#39; }}&gt;{record.childList1[0].pciName}&lt;/p&gt;              
      &lt;/div&gt;)}
/&gt;

type CheckListItemVOExtender = CheckListItemVO &amp; {
  sparPoint: string | number;
};

export type CheckListItemVO = CCheckListItem &amp; {
  childList1?:CheckListItemChildVO[]; 
}

export type CCheckListItem = {
  pciName?:string; 
}

Most of time, when I get the error, I put optional mark or did put "as Type" at the end (like second method), but it did not work in this case.

Could anyone help?

答案1

得分: 1

请查看表达式的可选链接

您还可以使用可选链接运算符与方括号表示法一起使用,允许将表达式作为属性名称传递
...
这对于数组特别有用,因为必须使用方括号来访问数组索引。

安全的语法如下...

record.childList1?.[0]?.pciName
  • 第一个 ?.[0] 用于因为 childList1 可能为未定义。
  • 第二个 ?.pciName 用于即使 childList1 是数组,它也可能为空,而 [0] 也可能为未定义。

但如果您只对第一个元素感兴趣,为什么 childList1 是一个数组呢?

通常,要迭代数组,例如

<div style={{ display: 'flex', flexDirection: 'column' }}>
  {record.childList1?.map(({ pciName }, index) => (
    <p key={index} style={{ whiteSpace: 'pre-wrap' }}>
      {pciName}
    </p>
  ))}
</div>
英文:

See Optional chaining with expressions.

> You can also use the optional chaining operator with bracket notation, which allows passing an expression as the property name
> ...
> This is particularly useful for arrays, since array indices must be accessed with brackets

The safe syntax would be...

record.childList1?.[0]?.pciName
  • The first ?.[0] is used because childList1 may be undefined.
  • The second ?.pciName is used because even if childList1 is an array, it may be empty and [0] could also be undefined.

But why is childList1 an array if you're only interested in the first element?

It's typical to iterate arrays, for example

&lt;div style={{ display: &#39;flex&#39;, flexDirection: &#39;column&#39; }}&gt;
  {record.childList1?.map(({ pciName }, index) =&gt; (
    &lt;p key={index} style={{ whiteSpace: &#39;pre-wrap&#39; }}&gt;
      {pciName}
    &lt;/p&gt;
  ))}
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年6月13日 12:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定