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.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
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#
let name: string = 'Asha';
function add(a: number, b: number): number { return a + b; }
Code example: A bug JavaScript misses, TypeScript catches#
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; }'
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#
- interface User describes the shape of a user object.
- user: User says the function expects exactly that shape.
- : string after the parentheses says the function returns a string.
- The typo user.emial is caught because User has no such property.
- 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#
Types vanish after compiling. Data from an API or a form can still be wrong at runtime — validate it (for example with zod).
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?
Show solution
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#
What happens to TypeScript types when the code is compiled?
Want more? Practise all TypeScript MCQs · TypeScript interview questions
My notes#
Saved in this browser.