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

2022-09-14 14:18:28 +08:00
 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;
};
1486 次点击
所在节点    问与答
9 条回复
Lqxc
2022-09-14 14:55:04 +08:00
还需要点上下文
cyrbuzz
2022-09-14 14:57:42 +08:00
XXX.specs?.item?.type === 'array'。
IvanLi127
2022-09-14 15:21:26 +08:00
要么你得先在前面加判断,判断出是 `XXX.specs` 是 `booleanDateType`;要么直接把 `XXX.specs` 强转成 `booleanDateType`。
lovedebug
2022-09-14 15:28:34 +08:00
你这是在操作类型啊,不是值
Leviathann
2022-09-14 15:28:39 +08:00
stringDateType 怎么定义的?
renmu
2022-09-14 16:59:22 +08:00
//@ts-ignore
xutao881
2022-09-14 17:04:26 +08:00
```
x?.x?.x?.item
```
可解 99% 的 not exist 问题
particlec
2022-09-14 17:27:37 +08:00
const arr = XXX.specs as ArrDateType;定死类型就行了
PerFectTime
2022-09-14 17:54:27 +08:00
//@ts-ignore

手动狗头

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

https://www.v2ex.com/t/879966

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

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

© 2021 V2EX