首页 > 解决方案 > 与 ES2020 无关的可选链接错误

问题描述

我在项目中使用可选链接时遇到问题。当我使用类似的东西时:

this.props.someString?.toUpperCase()

它不会导致 VS Code 中的任何语法错误,但会导致运行时错误

Module parse failed: Unexpected token (33:38) You may need an appropriate loader to handle this file type.

之前在 StackOverflow 上关于此问题的两篇文章都只声明将 tsconfig 中的目标更改为 es2020 或 es2018。我已经尝试过了,但它不起作用。我不确定要发布有关我的环境的其他上下文。我没有使用 Babel,我的 TS 版本是 3.9.9^。

编辑:tsconfig

{
  "compilerOptions": {
    "target": "es2020",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve",
    "noImplicitAny": false
  },
  "include": [
    "src"
  ]
}

标签: typescript

解决方案


推荐阅读