TypeScript · Getting Started

Introduction to TypeScript

TypeScript is JavaScript with a safety net. You describe what kind of data your code expects, and TypeScript warns you about mistakes while you type — before users ever see them.

Beginner 5 min Lesson 1 of 29Examples target TypeScript 5.x with "strict": true.
0/29 in TypeScript
On this page · 5 min · Beginner

Learning objectives#

  • Explain what TypeScript adds to JavaScript
  • Understand that types are checked at compile time and removed at runtime
  • Read a simple type annotation
  • Know where TypeScript is used

Explanation#

TypeScript (TS) is a superset of JavaScript made by Microsoft: every valid JavaScript file is already valid TypeScript. TS adds one thing on top — types.

A type describes what a value can be: a number, a string, an object with certain properties. You write it after a colon: let age: number = 25.

The TypeScript compiler (tsc) checks your code and then removes all the types, producing plain JavaScript that runs anywhere JavaScript runs. Types cost nothing at runtime.

The big win is feedback while you type: your editor underlines a typo in a property name, a missing argument or a possible undefined before you even save the file. Autocomplete becomes far smarter too.

Syntax#

TypeScript Illustrative
let name: string = 'Asha';
function add(a: number, b: number): number { return a + b; }

Code example: A bug JavaScript misses, TypeScript catches#

TypeScriptA bug JavaScript misses, TypeScript catches Illustrative
interface User {
  name: string;
  email: string;
}


function sendWelcome(user: User): string {
  return `Welcome, ${user.name}! We sent a mail to ${user.emial}`;
  //                                                   ~~~~~
  // ✗ Error: Property 'emial' does not exist on type 'User'. Did you mean 'email'?
}


sendWelcome({ name: 'Asha' });
//          ~~~~~~~~~~~~~~~~
// ✗ Error: Property 'email' is missing in type '{ name: string; }'
Expected output
The code does not compile. TypeScript reports both bugs — the misspelled property and the missing email — before the code ever runs. Plain JavaScript would have printed “…mail to undefined”.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. interface User describes the shape of a user object.
  2. user: User says the function expects exactly that shape.
  3. : string after the parentheses says the function returns a string.
  4. The typo user.emial is caught because User has no such property.
  5. The call is caught because the object is missing email.

Real-world use case#

Angular is written in TypeScript, most new React and Node.js projects use it, and companies like Microsoft, Google, Slack and Airbnb rely on it for large codebases.

Common mistakes#

Thinking TypeScript checks data at runtime

Types vanish after compiling. Data from an API or a form can still be wrong at runtime — validate it (for example with zod).

Using any to silence errors

any turns type checking off for that value. Fix the type instead; the error is usually a real bug.

Best practices#

  • Start new projects in TypeScript with strict mode on
  • Let your editor (VS Code) show errors as you type
  • Add types at boundaries: function parameters, API responses, props
  • Learn to read error messages — they usually say exactly what is wrong

Remember this#

TypeScript = JavaScript + types. Types are checked, then erased.

Errors at compile time are cheap; errors in production are expensive.

name: type — the colon introduces the type.

Practice exercise#

Your task: Add types to this function so that calling it with a string causes an error: function area(width, height) { return width * height; }

Need a hint?
Both parameters and the return value are numbers.
Show solution
TypeScript Illustrative
function area(width: number, height: number): number {
  return width * height;
}
area(5, 4);        // ✓ 20
area('5', 4);      // ✗ Argument of type 'string' is not assignable to parameter of type 'number'

Mini quiz#

Introduction to TypeScript quizMultiple choice Question 1 of 3

What happens to TypeScript types when the code is compiled?

Score so far: 0/0

Want more? Practise all TypeScript MCQs · TypeScript interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.