ts 问题很急,弄了一中午,求大佬看一下

2022 年 9 月 14 日
 particlec
代码如下:


XXX.specs.item.type === "array"
? XXXX
: [];


报错:Property 'item' does not exist on type 'stringDateType | numberDateType | arrDateType | structDateType'.   Property 'item' does not exist on type 'stringDateType'.


XXX: {
specs:
| stringDateType
| numberDateType
| arrDateType
| structDateType
| booleanDateType;
type:
| "int"
| "int8"
| "int16"
| "int32"
| "int64"
| "bool"
| "string"
| "float"
| "double"
| "array"
| "struct"
| "其他"
| unknown;
};
2488 次点击
所在节点    问与答
9 条回复
Lqxc
2022 年 9 月 14 日
还需要点上下文
cyrbuzz
2022 年 9 月 14 日
XXX.specs?.item?.type === 'array'。
IvanLi127
2022 年 9 月 14 日
要么你得先在前面加判断,判断出是 `XXX.specs` 是 `booleanDateType`;要么直接把 `XXX.specs` 强转成 `booleanDateType`。
lovedebug
2022 年 9 月 14 日
你这是在操作类型啊,不是值
Leviathann
2022 年 9 月 14 日
stringDateType 怎么定义的?
renmu
2022 年 9 月 14 日
//@ts-ignore
Balibabo
2022 年 9 月 14 日
```
x?.x?.x?.item
```
可解 99% 的 not exist 问题
particlec
2022 年 9 月 14 日
const arr = XXX.specs as ArrDateType;定死类型就行了
PerFectTime
2022 年 9 月 14 日
//@ts-ignore

手动狗头

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://v2ex.xtra.eu.org/t/879966

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX