1. What is TypeScript

TypeScript is a superset of JavaScript. That means every valid JavaScript file is also valid TypeScript. TypeScript simply adds one powerful feature on top: static types.

You write .ts files instead of .js files. A compiler (the TypeScript compiler, tsc) checks your code for type errors and then strips the types, producing plain JavaScript that any browser or Node.js environment can run.

How TypeScript Works
You write
TypeScript (.ts)
→
tsc checks types
Catches errors at compile time
→
Outputs JavaScript
Runs in browser / Node
🛒
UrbanCart Example
Finding a bug before it reaches users

In plain JavaScript, you could accidentally pass a string where a price number is expected — calculateTotal("free"). JavaScript happily runs it, and users get NaN in their cart total at midnight on a Friday. TypeScript catches this the moment you type it, before any code runs.

TypeScript is opt-in: You can adopt TypeScript gradually. Rename one file at a time from .js to .ts and add types incrementally. You don't need to rewrite everything at once.

2. Why TypeScript

Teams adopt TypeScript for three main reasons:

🐛

Catch bugs early

Type errors are caught at compile time — before the code runs, before QA, before users see it.

💡

Better autocomplete

Your IDE knows the shape of every object. It suggests the right properties and flags typos instantly.

📖

Self-documenting code

A function signature like addToCart(item: CartItem): void tells the reader everything they need to know.

Industry reality: Most large React, Next.js, and Node.js projects use TypeScript. Companies like Stripe, Airbnb, Slack, and every major Indian tech firm standardised on TypeScript years ago. It is now a baseline expectation for frontend roles.

3. Basic Types

TypeScript ships with a set of primitive types that map directly to JavaScript values, plus a few extra types for special situations.

Type Example value When to use
string"UrbanCart"Any text value
number49.99Integers and floats
booleantrueFlags, toggles, conditions
nullnullIntentional absence of a value
undefinedundefinedNot yet assigned
anyanythingOpt out of type checking (use sparingly)
unknownanything, but saferExternal data (API responses, user input)
neverno value everFunctions that always throw or infinite loop
voidundefinedFunctions that return nothing
types.ts Basic type examples
Tip: Avoid any — it completely disables type checking for that variable. Use unknown instead when you genuinely don't know the type; it forces you to check the type before using the value.

4. Type Annotations

A type annotation is the : type syntax you add after a variable name or parameter. It tells TypeScript exactly what type to expect.

annotations.ts Variable and function annotations
Annotating return types: Always annotate your function return types explicitly. It catches the bug where you forget to return something, or return the wrong type.

5. Type Inference

You don't have to annotate everything. TypeScript is smart — it infers the type from the value you assign. This keeps your code concise without losing safety.

inference.ts TypeScript infers types automatically
✅

When inference is enough

Annotate when the initial value doesn't make the type obvious, or when you want to enforce a specific type that's wider than the inferred one.

✏️

When to annotate explicitly

Always annotate function parameters and return types. Inference works for local variables; explicit types work better for public APIs.

6. Union Types

A union type says "this value can be one of several types." You write it with the | (pipe) operator.

unions.ts Union type examples
Type narrowing: When you use a union type, TypeScript requires you to narrow it before using type-specific operations. Use typeof, instanceof, or an if check to narrow the type. TypeScript tracks which branch you're in.

7. Literal Types

A literal type restricts a value to a specific set of exact values — not just any string, but only these strings. This is perfect for statuses, categories, and modes.

literals.ts Literal types with UrbanCart order status
Why this matters: Without literal types, a typo like 'shiped' would silently fail at runtime. With literal types, TypeScript flags it immediately — at the moment you type it.

8. Type Aliases

A type alias gives a name to any type definition. You use the type keyword. This is how you define the shape of custom objects — without the overhead of a class.

types.ts Type aliases for UrbanCart data models
type vs interface: TypeScript has both type aliases and interface declarations. For most objects you can use either interchangeably. A common convention: use interface for public API contracts (especially when extending), and type for unions, intersections, and utility types. In this track we primarily use type for consistency.

9. Arrays and Objects

TypeScript has clean syntax for typed arrays and record objects — both of which are used constantly in e-commerce data.

collections.ts Typed arrays and Record objects
📋

Product[]

An array of Product objects. TypeScript checks every push and access against the Product shape.

🗂️

Record<K, V>

A dictionary/map type. Record<string, number> is an object with string keys and number values — common for caches and lookup maps.

10. UrbanCart Types in Full

Putting it all together — here are the core TypeScript types that power UrbanCart. These types are the source of truth for everything: frontend components, API endpoints, and database queries all share the same definitions.

🏗️
UrbanCart Domain Model
Product, CartItem, Order — fully typed

In a real Next.js + TypeScript project, you'd put these in a src/types/index.ts file and import them everywhere. Every component that receives a product knows exactly what fields are available, and the compiler enforces it.

src/types/index.ts UrbanCart domain types

11. Try It Yourself — Product & CartItem Types

In this editor, TypeScript runs via Babel (which strips types and executes the JavaScript). You can type TypeScript annotations and see the output in the console panel on the right. Try changing a type and observe what happens.

How this editor works: Babel transforms TypeScript by stripping the types and running the resulting JavaScript. Full compile-time type errors are not shown — for that you'd use VS Code with the TypeScript extension. Think of this as a learning sandbox where you can explore the syntax and see console.log output.
Try It Yourself — UrbanCart Product & CartItem Types
console output
Click ▶ Run to execute...

12. Try It Yourself — Union & Literal Types

This editor demonstrates union types and literal types with the UrbanCart order status system. Notice how the OrderStatus type restricts which values are valid, and how TypeScript's type narrowing works inside switch statements.

Try It Yourself — OrderStatus Union & Literal Types
console output
Click ▶ Run to execute...

Quiz

Question 1 of 5
What does TypeScript compile to?
Question 2 of 5
What is a union type?
Question 3 of 5
What is the difference between any and unknown?
Question 4 of 5
When does TypeScript check types?
Question 5 of 5
What is a type alias?
0
/ 5
Great job!

You completed the TypeScript Fundamentals quiz.

Next Lesson

TypeScript with React →
You've got the TypeScript foundations. Next: adding types to React components, props, hooks, and events.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just levelled up from JavaScript to TypeScript.
Now build a career that's bulletproof.

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →