Prerequisites
- Node.js 14.17 or higher installed
- TypeScript installed (see Installation)
Create Your First TypeScript File
Initialize TypeScript Configuration
Create atsconfig.json file to configure the TypeScript compiler:
tsconfig.json file with default settings and helpful comments. For this quickstart, create a minimal configuration:
tsconfig.json
Configuration options explained
Configuration options explained
- target: ECMAScript version for the output JavaScript (ES2020, ES2015, etc.)
- module: Module system to use (NodeNext, CommonJS, ES2015, etc.)
- outDir: Output directory for compiled JavaScript files
- rootDir: Root directory of TypeScript source files
- strict: Enable all strict type-checking options
- esModuleInterop: Enables better CommonJS/ES module interoperability
- skipLibCheck: Skip type checking of declaration files
index.ts file into a src/ directory to match the configuration:
Compile TypeScript
Compile your TypeScript code to JavaScript:src/index.ts and output JavaScript to dist/index.js.
View the compiled output
Check the generated JavaScript:dist/index.js
Run Your Code
Execute the compiled JavaScript:Watch Mode
During development, use watch mode to automatically recompile when files change:.ts files.
Press
Ctrl+C to stop watch mode.Type Checking in Action
Let’s see TypeScript’s type checking in action. Add this code tosrc/index.ts:
src/index.ts
Common Compiler Options
Usefultsc command-line options:
Using with ts-node
For running TypeScript directly without compiling first, installts-node:
Project Structure
Your project should now look like this:Add Build Scripts
Updatepackage.json to add convenient build scripts:
package.json
Next Steps
tsconfig.json
Deep dive into compiler configuration options
Compiler
Learn about the TypeScript compiler
Module Resolution
Understand module resolution and imports
CLI Reference
Complete command-line interface documentation