Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 465
Yudhistira Arya Avatar asked Jan 22 '20 17:01

Yudhistira Arya


1 Answers

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:(

like image 77
lena Avatar answered Nov 05 '22 02:11

lena