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.
TypeScript (.ts)
Catches errors at compile time
Runs in browser / Node
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.
.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.
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 |
number | 49.99 | Integers and floats |
boolean | true | Flags, toggles, conditions |
null | null | Intentional absence of a value |
undefined | undefined | Not yet assigned |
any | anything | Opt out of type checking (use sparingly) |
unknown | anything, but safer | External data (API responses, user input) |
never | no value ever | Functions that always throw or infinite loop |
void | undefined | Functions that return nothing |
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.
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.
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.
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.
'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.
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.
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.
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.
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.
console.log output.
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.
Quiz
any and unknown?You completed the TypeScript Fundamentals quiz.
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 →