"Object is possibly 'null'.",尽管可选链接



即使我使用可选链接和TypeScript 4.6.3,我也会得到Object is possibly 'null'. ts(2531)

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" }]
}

我做错了什么?

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

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

相关内容

最新更新