Remember that the program file that you want to run should have the focus before you execute, otherwise you'll get the 'Cannot find Javascript' error dialog. Especially when the file you are executing is not a Javascript or a Typescript file. Becuase, in the default launch.json file, configuration "program": "${file}" is specified. That means the file that is currently being displayed on the screen will be executed.
So for example: If you are in a non-js or non-ts file, say, tsconfig.json or launch.json and you click Run command, then you'll get that error dialog.
Properly setting up a Typescript project in VS Code
Make sure you have Typescript and Node.js installed on your machine before creating the project.
1.Intialize in the terminal
In the terminal create a new folder for your project.
mkdir MyProject
Change the current directory to the folder you made above.
cd MyProject
Initialize the project to enable Typescript. This will create the tsconfig.json file.
tsc --init
Open this folder in VS Code. This command works from MacOS. You can open manually too.
code .
2.Configure the output directory
Now go to the file tsconfig.json and add the following lines to the compilerOptions. Yes, you need to specify the output directory here in tsconfig.json instead of launch.json. VS Code will always look for files in default outDir of launch.json which is ${workspaceFolder}/**/*.js.
"outDir": "./out", /* Specify .js output files. */
"sourceMap": true /* Generate corresponding .map files. */
Running/Debugging the project
Write a simple program to test and run: welcome.ts
console.log('Welcome to Typescript');
1.Build
Now Click Run Build Task (Shift + Command(Ctrl) + B) from the Terminal menu of the VS Code and type the following command and press enter:
tsc: watch - tsconfig.json
You need to Run Build Task once when you first open the project. This will start watching for code changes in the project.
2.Run
Now go to the Typescript program that you want to run (Make sure your program file .ts has the focus).
From the Run menu:
Click Start Debugging for debug (F5)
OR
Click Run Without Debugging (Ctrl + F5)
Output will be displayed in the Debug Console.
That's it. It may seem overwhelming at first, but it's easy once you get used to it.