首页 > 解决方案 > 调试会话期间的打字稿未定义常量

问题描述

我有以下带有 Typescript项目的 Node.js,它运行得非常好。但是,在调试会话期间,我注意到从另一个 Typescript 文件中导出的常量始终是undefined

在此处输入图像描述

我有点怀疑这是由于生成的源映射names中的空数组:

{
  "version": 3,
  "file": "main.js",
  "sourceRoot": "",
  "sources": [
    "../src/main.ts"
  ],
  "names": [], // <---- empty
  "mappings": ...details omitted... 
}

有没有办法从Typescript编译器或其他解决方案来解决这个调试问题?以下是我的tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true, 
    "target": "es2017",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "sourceMap": true,
    "allowJs": true,
    "outDir": "dist",
    "baseUrl": ".",
    "typeRoots": [
      "./node_modules/@types"
    ],
    "types": [
      "node" 
    ],
    "paths": {
      "*": [
        "node_modules/*"
      ]
    }
  },
  "include": [
    "src/**/*"
  ]
}

我正在使用 Typescript 3.7.5。

该项目只是通过 Webstorm 的 Node runner 运行:

在此处输入图像描述

旁注:当我调试一个运行的 Angular 应用程序时,这个问题没有发生ng serve。不确定ng serve与标准有何不同tsc

标签: node.jstypescriptwebstorm

解决方案


是的,"names": []sourcemaps 中的空是问题 -tsc将您的命名导入编译为category_1.ANIMAL_TYPE,并且没有名称映射,因此在调试器中未定义变量...在 VSCode 中调试时您将面临同样的问题,例如:

在此处输入图像描述

您可以检查category_1对象以查看值:

在此处输入图像描述

我不知道有什么方法可以改变tsc行为:(


推荐阅读