TS18048: ” 可能是 ‘undefined’

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

TS18048: '' is possibly 'undefined'

问题

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

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

但仍然出现相同的错误。

这是我的代码:

  1. <Table.Column
  2. key='childList1'
  3. dataIndex='childList1'
  4. title={<div style={{ textAlign: 'center' }}>standard</div>}
  5. render={(text, record: CheckListItemVOExtender) => (
  6. <div style={{ display: 'flex', flexDirection: 'column' }}>
  7. <p style={{ whiteSpace: 'pre-wrap' }}>{record.childList1[0]?.pciName}</p>
  8. </div>)}
  9. />
  10. type CheckListItemVOExtender = CheckListItemVO & {
  11. sparPoint: string | number;
  12. };
  13. export type CheckListItemVO = CCheckListItem & {
  14. childList1?: CheckListItemChildVO[];
  15. }
  16. export type CCheckListItem = {
  17. pciName?: string;
  18. }

大多数情况下,当我遇到这种错误时,我会使用可选标记或在末尾使用"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

  1. &lt;Table.Column
  2. key=&#39;childList1&#39;
  3. dataIndex=&#39;childList1&#39;
  4. title={&lt;div style={{ textAlign: &#39;center&#39; }}&gt;standard&lt;/div&gt;}
  5. render={(text, record: CheckListItemVOExtender) =&gt; (
  6. &lt;div style={{ display: &#39;flex&#39;, flexDirection: &#39;column&#39; }}&gt;
  7. &lt;p style={{ whiteSpace: &#39;pre-wrap&#39; }}&gt;{record.childList1[0].pciName}&lt;/p&gt;
  8. &lt;/div&gt;)}
  9. /&gt;
  10. type CheckListItemVOExtender = CheckListItemVO &amp; {
  11. sparPoint: string | number;
  12. };
  13. export type CheckListItemVO = CCheckListItem &amp; {
  14. childList1?:CheckListItemChildVO[];
  15. }
  16. export type CCheckListItem = {
  17. pciName?:string;
  18. }

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

请查看表达式的可选链接

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

安全的语法如下...

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

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

通常,要迭代数组,例如

  1. <div style={{ display: 'flex', flexDirection: 'column' }}>
  2. {record.childList1?.map(({ pciName }, index) => (
  3. <p key={index} style={{ whiteSpace: 'pre-wrap' }}>
  4. {pciName}
  5. </p>
  6. ))}
  7. </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...

  1. 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

  1. &lt;div style={{ display: &#39;flex&#39;, flexDirection: &#39;column&#39; }}&gt;
  2. {record.childList1?.map(({ pciName }, index) =&gt; (
  3. &lt;p key={index} style={{ whiteSpace: &#39;pre-wrap&#39; }}&gt;
  4. {pciName}
  5. &lt;/p&gt;
  6. ))}
  7. &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:

确定