TSTypeScript

Learn TypeScript – tutorial, cheatsheet & exercises

TypeScript is JavaScript with a type system. You write normal JavaScript and add types such as string, number or your own shapes for objects. The compiler checks that everything fits together, reports errors right in the editor as you type, and in the end produces plain JavaScript.

This way you catch typos, wrong arguments and forgotten null cases before the code runs, and the editor can support you with precise autocompletion and safe renaming. TypeScript is the standard tool for larger web apps, frameworks (Angular, Next.js, NestJS) and Node.js backends. One important thing to know: types only exist at compile time – at runtime everything is JavaScript.

Large web appsFrameworksNode.js backends

Hello World in TypeScript

const gruss: string = "Hello World!";
console.log(gruss);

Like JavaScript, only with the type annotation : string after the name. tsc compiles, node runs the result.

How to get started with TypeScript

TypeScript needs Node.js (nodejs.org, or brew install node). After that you install the compiler per project as a development dependency:

mkdir mein-projekt && cd mein-projekt
npm init -y
npm install --save-dev typescript
npx tsc --version

Create a configuration file tsconfig.json with sensible defaults (be sure to keep "strict": true enabled):

npx tsc --init

Write a file hallo.ts, compile it with tsc and run the resulting JavaScript with Node:

npx tsc hallo.ts      # creates hallo.js
node hallo.js

More convenient for experimenting: tsx runs TypeScript directly (without type checking), and newer Node.js versions (from 22.18 or 23.6) can even start .ts files themselves by simply stripping the types. Deno and Bun also support TypeScript out of the box.

npx tsx hallo.ts
node hallo.ts          # Node.js 22.18+ (only "erasable" syntax, no enums)
npx tsc --noEmit       # only check, produce nothing (ideal for CI)
TipWithout installing anything you can try code in the playground at typescriptlang.org/play. Visual Studio Code has TypeScript support built in. The code on this page cannot be run in the browser – copy it into the playground or into your own .ts file.

At a glance

First released2012
CreatorAnders Hejlsberg (Microsoft)
ParadigmMulti-paradigm
TypingStatic (structural), gradual
ExecutionTranspiled to JavaScript
File extension.ts

Test & practice

Interactive quiz, timed exam with certificate, and exercises with an editor:

TypeScript compared

Hello World: TypeScript comparedVariables: TypeScript comparedConditionals: TypeScript comparedLoops: TypeScript comparedFunctions: TypeScript comparedLists and Arrays: TypeScript comparedDictionaries and Maps: TypeScript comparedClasses and Objects: TypeScript comparedError Handling: TypeScript comparedReading Files: TypeScript compared