【问题标题】:VSCode isn't showing inline typescript errorsVSCode 没有显示内联打字稿错误
【发布时间】:2021-05-07 14:01:40
【问题描述】:

我刚刚安装了 Deno 1.9.2 并在我的 PC 上打开了一个空白文件夹。我正在学习有关 TypeScript 基础知识的教程。这就是我的问题所在。

const someFunc = (n: number) => {
  if (n % 2 === 0) {
    return "even"
  }
}

const value = someFunc(4)
value.substring(1)

在某一时刻,VSCode 给老师一个关于值的内联警告,说 Object is possibly 'undefined'. 我环顾四周,并被告知将我的 VSCode typescript.validate.enable 更改为 true。我已经这样做了,并且多次重新启动了 VSCode。当我使用deno run index.ts 运行我的代码时,我得到了我的错误。有什么想法吗?

【问题讨论】:

    标签: typescript inline deno


    【解决方案1】:

    此错误与 VSCode 或 Deno 无关。 在您提供的代码中,函数someFunc 的返回类型为string | undefined。我猜你的tsconfig.jsonstrictFunctionTypes: true
    您可以通过以下方式修复此错误:

    正确处理所有案件 -

    const someFunc = (n: number) => {
      if (n % 2 === 0) {
        return 'even';
      }
      return ''
    };
    

    或者在您的tsconfig.json 中禁用strictFunctionTypes -

    {
      "compilerOptions": {
        "strictFunctionTypes": false,
        "plugins": [
          {
            "name": "typescript-deno-plugin",
            "enable": true, // default is `true`
            "importmap": "import_map.json"
          }
        ]
      }
    }
    

    虽然我不确定 Deno 是否可以在没有 strictFunctionTypes 的情况下工作。

    【讨论】:

    • 非常感谢。有了这个小脚本,我没有使用 tsconfig。我只是相信 deno 会处理它。你知道是否有一个 tsconfig 可以查看 deno 的安装位置吗?
    • @NaeNate 这是我找到的manual。看起来您可以使用 config 选项指定 tsconfig 位置。欢迎来到 Stack Overflow 社区。​​span>
    猜你喜欢
    • 2017-10-10
    • 2018-03-19
    • 2020-04-15
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多