调试会话期间打字稿未定义的常量

Yud*_*rya 5 node.js webstorm typescript

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

在此处输入图片说明

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

{
  "version": 3,
  "file": "main.js",
  "sourceRoot": "",
  "sources": [
    "../src/main.ts"
  ],
  "names": [], // <---- empty
  "mappings": ...details omitted... 
}
Run Code Online (Sandbox Code Playgroud)

有没有办法从 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/**/*"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 Typescript 3.7.5。

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

在此处输入图片说明

旁注:当我调试通过ng serve. 不确定ng serve与标准有什么不同tsc

len*_*ena 5

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

在此输入图像描述

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

在此输入图像描述

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

  • 刚刚发现这显然是一个 4 年前的问题尚未解决:https://github.com/microsoft/TypeScript/issues/9627 (2认同)