代码之家  ›  专栏  ›  技术社区  ›  leonheess

“对象可能为‘null’。”尽管可选链接[重复]

  •  0
  • leonheess  · 技术社区  · 4 年前

    我越来越 Object is possibly 'null'. ts(2531) 尽管我使用可选链接和TypeScript 4.6.3。

    const path = fileInput?.files[0]?.path
    

    tsconfig。json:

    {
      "compilerOptions": {
        "target": "esnext",
        "useDefineForClassFields": true,
        "module": "esnext",
        "moduleResolution": "node",
        "strict": true,
        "jsx": "preserve",
        "sourceMap": true,
        "resolveJsonModule": true,
        "isolatedModules": true,
        "esModuleInterop": true,
        "lib": ["esnext", "dom"]
      },
      "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
      "references": [{ "path": "./tsconfig.node.json" }]
    }
    

    我做错了什么?

    1 回复  |  直到 4 年前
        1
  •  1
  •   Unmitigated    4 年前

    对数组索引访问也可以使用可选的链接。

    const path = fileInput?.files?.[0]?.path
    
    推荐文章