Skip to main content
This guide walks you through creating, compiling, and running your first TypeScript project.

Prerequisites

  • Node.js 14.17 or higher installed
  • TypeScript installed (see Installation)

Create Your First TypeScript File

1

Create a project directory

2

Initialize npm

This creates a package.json file for your project.
3

Install TypeScript

4

Create a TypeScript file

Create a file called index.ts with the following content:
index.ts
The : string syntax adds type annotations. TypeScript will check that you only pass strings to the greet function.

Initialize TypeScript Configuration

Create a tsconfig.json file to configure the TypeScript compiler:
This generates a tsconfig.json file with default settings and helpful comments. For this quickstart, create a minimal configuration:
tsconfig.json
  • 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
Move your index.ts file into a src/ directory to match the configuration:

Compile TypeScript

Compile your TypeScript code to JavaScript:
TypeScript will compile src/index.ts and output JavaScript to dist/index.js.
If compilation succeeds with no output, your code compiled successfully! TypeScript only shows messages for errors and warnings.

View the compiled output

Check the generated JavaScript:
You’ll see something like:
dist/index.js
Notice how the type annotations have been removed - they’re only used during compilation for type checking.

Run Your Code

Execute the compiled JavaScript:
Output:

Watch Mode

During development, use watch mode to automatically recompile when files change:
You’ll see output like:
Now TypeScript will automatically recompile whenever you save changes to your .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 to src/index.ts:
src/index.ts
Compile again:
You’ll see a type error:
This is TypeScript preventing a potential runtime bug at compile time!

Common Compiler Options

Useful tsc command-line options:

Using with ts-node

For running TypeScript directly without compiling first, install ts-node:
Run TypeScript files directly:
Output:
ts-node is great for development but not recommended for production. Always compile to JavaScript for production deployments.

Project Structure

Your project should now look like this:

Add Build Scripts

Update package.json to add convenient build scripts:
package.json
Now you can use:

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

Additional Resources