keywords: TypeScript, JavaScript Engine, Nodejs, Chrome, V8, Notes

Setup compilation environment for Visual Studio Code

1, Install compiler (tsc) of TypeScript:

npm install typescript -g

2, Create a directory as your project, named myproj.

3, Create a TypeScript source file under myproj, named hello.ts, and print a message in console:

let message: string = 'Hello';
console.log(message);

then compile source with command:

tsc hello.ts​

4, Address to directory myproj in command, then generate tsconfig.json using command:

tsc --init

Reference:
https://zhuanlan.zhihu.com/p/115675528

5, Go to your project directory and excute commands:

npm install typescript --save
npm install ts-node --save-dev

Reference:
https://cloud.tencent.com/developer/article/1760857

If you got error:

Error: Cannot find module 'typescript'

Go to your project directory and link typescript to the project:

cd myproj

npm link typescript

6, Run npx ts-node to test your source code:

npx ts-node hello.ts

Reference:
https://stackoverflow.com/a/65508481/1645289

7, Install extension TypeScript Debugger, then click Run -> Add Configuration -> select TS Debug:

8, Open directory myproj in Visual Studio Code, then press F5 or click Run -> Start Debugging.

Issue:

If you get error on Windows while Start Debugging:

tsc.ps1 cannot be loaded because running scripts is disabled on this system

Excute command using PowerShell as administrator, and select option A(ll):

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Reference:
https://stackoverflow.com/a/58801137/1645289

Now you would run debugging successfully.

Setup compilation environment for Intellij IDEA

Do first five above-side steps, then compile TypeScript source by clicking lable which was in the bottom of editor viewport, then debug your code.

TypeScript plugin is only usable for Intellij IDEA Ultimate Edition and WebStorm.

Issues

Issue 2:

If you get error while Start Debugging:

D:\SDKs\nodejs\node.exe -r ts-node/register .vscode\launch.json
SyntaxError: D:\myproj\.vscode\launch.json: Unexpected token / in JSON at position 7
    at parse (<anonymous>)
    at Object.Module._extensions..json (internal/modules/cjs/loader.js:1128:22)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47
Process exited with code 1

Or:

Debugger attached.
Waiting for the debugger to disconnect...
d:\myproj\hello.ts:1
let message: string = 'Hello World AAA';
           ^

SyntaxError: Unexpected token ':'
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47
Process exited with code 1

It’s the bug of extentsion TypeScript Debugger of Visual Studio Code.
https://github.com/microsoft/vscode/issues/45616

PuerTS

Tips & Tricks

Delegate: Invoke TypeScript function from C++ using PuerTS
https://github.com/Tencent/puerts/issues/247

Examples

Unreal example: How to debug TypeScript based on Unreal PuerTS in IntelliJ IDEA
https://github.com/dawnarc/PuertsGame

C++ server example:《c++游戏服务器嵌入v8 js引擎胎教级教程》配套代码
https://github.com/chexiongsheng/v8_embedding_test

Tools & Plugins

IntelliJ IDEA Plugins

IntelliJ IDEA (and WebStorm…) plugin to support ‘TypeScript’ as a run configuration
https://github.com/bluelovers/idea-run-typescript

References

Performance

Why is TypeScript surpassing Python?
https://blog.logrocket.com/why-is-typescript-surpassing-python/


“As a child I felt myself to be alone, and I am still, because I know things and must hint at things which others apparently know nothing of, and for the most part do not want to know.” ― Carl Gustav Jung, Memories, Dreams, Reflections