3

I am trying a basic ts setup to use top-level await and it works (prints the result from the api) if I run tsc && node dist/main.js, but when I run npx tsc - it does nothing but generate dist folder with main.js and main.js.map. I am trying to understand why npx tsc doesn't work and what I am doing wrong.

Here is my setup

tsconfig.json

{
  "compilerOptions": {
    "esModuleInterop": true,
    "preserveConstEnums": true,
    "module": "es2022",
    "target": "ES2021",
    "outDir": "./dist",
    "strict": true,
    "sourceMap": true,
    "types": [
      "node"
    ],
    "moduleResolution": "Node",
    "allowJs": true
  },
  "include": [
    "src"
  ],
  "exclude": [
    "node_modules"
  ]
}

src/main.ts

import axios from "axios"

let api = 'https://www.boredapi.com/api/activity'
let response = await axios.get(api)

console.log(`You could ${response.data.activity}`)
```
Olga
  • 73
  • 1
  • 8
  • Could not replicate, I get `main.js` and `main.d.ts` in `dist/`. – jonrsharpe Apr 19 '22 at 08:31
  • @jonrsharpe, it does generate main.js and main.js.map. I was following instructions from [this answer](https://stackoverflow.com/a/71526250/12447423) and from what I understood the code in `main.ts` should be executed as well. – Olga Apr 19 '22 at 08:53
  • 3
    You have misunderstood, `tsc` just transpiles TypeScript to JavaScript, `tsc && node dist/main.js`, which you already know works, would be compile _then run_. – jonrsharpe Apr 19 '22 at 08:55

1 Answers1

-1

The tsc command's purpose is to transpile typescript to javascript, just run tsc && node dist/main.js to transpile and then have node start the main.js file in the dist folder.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Promise Ihunna
  • 293
  • 3
  • 8