particlec
0.01D
V2EX  ›  问与答

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

  •  
  •   particlec · Sep 14, 2022 · 2933 views
    This topic created in 1511 days ago, the information mentioned may be changed or developed.
    代码如下:


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

    手动狗头
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2372 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 1149ms · UTC 15:27 · PVG 23:27 · LAX 07:27 · JFK 10:27
    ♥ Do have faith in what you're doing.