TypeScript Tutorial

This TypeScript tutorial teaches you how to add static types to JavaScript so you catch mistakes before your code runs. TypeScript is a superset of JavaScript made by Microsoft: every JavaScript program is valid TypeScript, and TypeScript adds type annotations, interfaces, generics and more.

TypeScript code is compiled (transpiled) to plain JavaScript, so it runs anywhere JavaScript runs: browsers, Node.js, Deno, Bun. Types disappear after compilation; they exist to help you and your editor while you write code.

Your First TypeScript Example

Here a function declares that it takes a string and returns a string. If you call it with a number, the TypeScript compiler reports an error before anything runs.

function greet(name: string): string {
  return `Hello, ${name}!`;
}

console.log(greet("TypeScript"));
// greet(42);
// Error: Argument of type 'number' is not assignable to parameter of type 'string'.
Hello, TypeScript!

Why Learn TypeScript?

  • Catch typos and wrong types at compile time instead of in production
  • Excellent editor autocompletion and inline documentation
  • Safer refactoring: renaming a property updates every usage
  • Types act as living documentation for your team
  • Used by default in Angular, and widely in React, Vue, Node.js and Next.js projects

TypeScript vs JavaScript

JavaScript checks types only while running, so a bug like reading user.nmae instead of user.name returns undefined silently. TypeScript checks your code statically and flags the typo immediately.

const user = { name: "Sara", age: 29 };

console.log(user.name.toUpperCase());
// console.log(user.nmae);
// Error: Property 'nmae' does not exist on type '{ name: string; age: number; }'.
//        Did you mean 'name'?
SARA

What This TypeScript Tutorial Covers

You will set up the compiler, learn basic types, arrays and tuples, object types, interfaces and type aliases, typed functions, union and literal types, narrowing, enums, classes, generics, utility types, and advanced operators like keyof and typeof. The last chapter gives you exercises that combine everything. Examples use TypeScript 5 and show the output of the compiled JavaScript. You should know basic JavaScript first.

Frequently asked questions

Is TypeScript better than JavaScript?

For medium and large projects most teams find it better because it catches bugs early and improves tooling. For tiny scripts, plain JavaScript is often enough.

Should I learn JavaScript before TypeScript?

Yes. TypeScript is JavaScript plus types, so you need to understand JavaScript fundamentals like functions, objects and promises first.

Does TypeScript run in the browser?

Not directly. It is compiled to JavaScript first, using tsc or a build tool like Vite, and the browser runs that JavaScript.