Home Part 1: Create a TypeScript project with VSCode
Post
Cancel

Part 1: Create a TypeScript project with VSCode

It is pretty easy to create a TypeScript project but the problem (at least for me) is the configuration with VSCode in order for the debugger works. Following I have fetched some configuration that helps create a TypeScript project and configure VSCode in order that you can simply use your regular shortcuts to start debugging it.

For completeness’ sake, you first have to install TypeScript of course.  I assume you have NodeJS already installed (if not Google or DuckDuckGo NodeJS and you should find very helpful entries).

Install typescript

1
npm install -g typescript

Create the Project

Create a new folder and change it.

1
2
mkdir project 
cd project

Then init a NodeJS project with npm, which asks you a few things and creates the package.json file afterward.

1
npm init

Then initialize TypeScript, which creates the tsconfig.json with your compiler options.

1
npx tsc --init

VSCode Configuration

Create a .vscode folder and the two files launch.json and tasks.json. By now, your folder structure should look something like the following:

1
2
3
4
5
|- .vscode 
  |- launch.json 
  |- tasks.json 
|- tsconfig.json 
|- package.json

Now use the following settings for the launch.json and tasks.json files. The launch.json tells VSCode what to do when you start the debugging (F5), and the tasks.json tells it to run TypeScript without tsconfig.json options.

```json launch.json { “configurations”: [ { “name”: “Launch via NPM”, “request”: “launch”, “runtimeArgs”: [ “run”, “dev” ], “runtimeExecutable”: “npm”, “skipFiles”: [ “<node_internals>/**” ], “type”: “node” }, ] }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
```json tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "typescript",
            "tsconfig": "tsconfig.json",
            "problemMatcher": [
                "$tsc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Now in your package.json file, you should add the dev script so we can start it.

json package.json "scripts": { "test": "echo \"Error: no test specified\" &amp;&amp; exit 1", "dev": "ts-node-dev ./src/app.ts" }

Now if you try the in-build debugger in VSCode you should be able to debug it like any other application.

For the next part, we will look at how to create a docker container that runs the completed TypeScript project so stay tuned.

Sources & Ressources

This post is licensed under CC BY 4.0 by the author.
Newsletter
Contents

QT on WSL2: reMarkable coding on Windows

Upgrading Flashforge Adventurer 3 Lite with Filament sensor