V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
particlec
V2EX  ›  问与答

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

  •  
  •   particlec · 2022-09-14 14:18:28 +08:00 · 1452 次点击
    这是一个创建于 561 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码如下:


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

    手动狗头
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1041 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 22:24 · PVG 06:24 · LAX 15:24 · JFK 18:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.