What is TypeScript and Why Use It?
Short answer: TypeScript is JavaScript with optional static types. You write TypeScript, the compiler checks your types and reports mistakes while you code, and then it produces plain JavaScript that runs anywhere JavaScript runs. It was created by Microsoft and is now widely used in front-end and back-end projects.
The problem types solve
// JavaScript
function total(price, quantity) {
return price * quantity;
}
total("10", 3); // 30, works by accident
total(10); // NaN, no warning
total({}, 3); // NaN
JavaScript only finds such mistakes when the code runs, often in production. TypeScript finds them as you type:
// TypeScript
function total(price: number, quantity: number): number {
return price * quantity;
}
total("10", 3); // Error: Argument of type 'string' is not assignable to type 'number'
total(10); // Error: Expected 2 arguments, but got 1
How TypeScript works
- You write
.ts(or.tsx) files. - The compiler (
tsc) checks types and emits.jsfiles; types are removed. - The JavaScript runs in the browser or Node.js as usual.
Types exist only during development; there is no runtime cost. Many tools (Vite, Next.js, esbuild) compile TypeScript for you.
Everyday TypeScript
// primitive types and inference
let name: string = "Asha";
let age = 30; // inferred as number
// arrays and objects
const scores: number[] = [90, 85];
interface User {
id: number;
name: string;
email?: string; // optional
}
const user: User = { id: 1, name: "Asha" };
// union types
type Id = string | number;
// function types
function greet(user: User): string {
return "Hello, " + user.name;
}
Learn the choice between the two main ways to describe shapes in interfaces vs types, then reuse logic safely with generics.
Why use TypeScript
- Catch bugs early: typos, wrong arguments and missing properties are flagged immediately.
- Better editor support: accurate autocomplete, safe renaming and instant documentation.
- Easier refactoring: change a function signature and see everything that breaks.
- Self-documenting code: types describe what functions expect and return.
- Scales with teams: large code bases stay understandable.
- Gradual adoption: you can migrate a JavaScript project file by file.
Trade-offs
| Cost | Comment |
|---|---|
| Learning curve | Generics and advanced types take time |
| Build step | Needs a compiler or bundler setup |
| More code | Type annotations add lines, though inference reduces this |
| Types are not runtime checks | Data from APIs or forms must still be validated |
Getting started
npm install --save-dev typescript
npx tsc --init # creates tsconfig.json
npx tsc # compile
In tsconfig.json, turn on "strict": true for the best safety. For a step-by-step setup, read how to set up TypeScript in a project.
Migrating from JavaScript
- Add TypeScript and a
tsconfig.jsonwithallowJs: true. - Rename files from
.jsto.tsone at a time. - Fix type errors, using
unknowninstead ofanywhere you can. - Tighten settings (
strict) over time.
TypeScript with frameworks
React, Next.js, Angular, Vue and Node.js all support TypeScript well. Angular uses it by default. See what React is and utility types for helpful built-in tools.
FAQs
Is TypeScript a separate language?
It is a superset of JavaScript: all valid JavaScript is valid TypeScript, plus types and a few extra features.
Can browsers run TypeScript?
No. It must be compiled to JavaScript first.
Should I learn JavaScript first?
Yes. TypeScript builds on JavaScript knowledge. See what JavaScript is.
Is TypeScript slower?
The output is ordinary JavaScript, so runtime speed is the same. Only compilation adds build time.
