Get Started with TypeScript
TypeScript is JavaScript with types, improved syntax, and better tooling.
Possible Setups:
- Basic setup: using the default tool
- Better setup (recommended): using the modern tool that is better and more efficient
Basic Setup
TypeScript can be installed from: https://www.typescriptlang.org/download/
Once installed, you can check the version:
tsc --version
1. Project Initialization
Initialize a project:
tsc --init
This creates a tsconfig.json file for configuration.
2. Running a TypeScript File
Create a main.ts file with the code:
console.log("Hello World");
Run the file:
# transpile ts into js
tsc # or tsc main.ts
# run the transpiled code
node main.js
When getting back to a project
# transpile
tsc
# run
node main.js
Better Setup
Instead of using the default TypeScript tsc, you can use tsx
tsx can be installed from: https://tsx.is/getting-started
Once installed, you can check the version:
tsx --version
1. Project Initialization
Initializing and creating a tsconfig.json file is optional.
2. Running a TypeScript File
Create a main.ts file with the code:
console.log("Hello World");
Run the file:
tsx main.ts
When getting back to a project
tsx main.ts