Yudhistira Arya
Yudhistira Arya

Reputation: 3671

Typescript undefined constant during debugging session

I have the following Node.js with Typescript project that runs absolutely fine. However, during debugging session, I noticed that exported constant from another Typescript file are always undefined:

enter image description here

I kinda suspect that this is due to the empty names array in the generated source maps:

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

Is there a way to generate proper source map from a Typescript compiler or maybe other solution to solve this debugging issue? Below are my 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/**/*"
  ]
}

I'm using Typescript 3.7.5.

The project was simply run through Webstorm's Node runner:

enter image description here

Side note: This issue didn't happen when I debug an Angular application that is run through ng serve. Not sure what ng serve do differently from standard tsc.

Upvotes: 6

Views: 1053

Answers (1)

lena
lena

Reputation: 93748

Yes, the empty "names": [] in sourcemaps is the issue - tsc compiles your named import to category_1.ANIMAL_TYPE, and there are no name mappings, so the variable is undefined in debugger... You will face the same issue when debugging in VSCode, for example:

enter image description here

You can inspect the category_1 object to see the values:

enter image description here

I'm not aware of any way to alter tsc behavior:(

Upvotes: 5

Related Questions