A clear and practical introduction to TypeScript for JavaScript developers. Learn how to write safer, more predictable code by adding types, preventing silent bugs, and building applications that scale with confidence.
This is Part 4 of the series Web Development: From Zero to Production
Before diving into TypeScript, make sure you’re comfortable with HTML and JavaScript fundamentals.
JavaScript gives freedom, Sometimes too much freedom.
You can pass the wrong data, misspell a property.
Return Something unexpected. & only discover the mistake when your app is running. That’s fine for small projects.
But as applications grow, small mistakes become expensive bugs.
This is where Typescript comes in.
TypeScript doesn’t replace JavaScript. It strengthens it.
It adds a layer of clarity — rules about your data — so errors are caught before your code runs.
In this guide, you’ll learn what TypeScript actually is, why modern teams use it, and how to start using it without feeling overwhelmed.
If you already know JavaScript, then you should have to add types to it.
Let’s level up.
What is TypeScript?
TypeScript is just types with JavaScript.
JS is loosely typed language, TypeScript allows specifying the types of data being passed around within the code.
TypeScript is a superset of JavaScript.
TypeScript code is compiled into JavaScript because browsers can only execute JavaScript.
TS is type checker, because of type checker we get consistency in code.
TypeScript = JavaScript + static typing
Example:
let age: number = 10; // The age variable will always hold number.
// age is a variable name
// : number is a type annotation
// Later if you wanna change it to some other type then:
age = "Luffy"; // This gives an error
So now let’s see how to install and use TypeScript:
npm install -g typescript
This installs the typescript, now can you can create a file and .ts and at the end of it and write TS code.
To initalize the typescript
npx tsx --init
Which creates a tsconfig.json file.
And to execute the code, you have to commands:
npx tsc hello.ts // this will generate hello.js file in same folder
node hello.js // executes the code
Basic Types
Types describe nature of data.
Primitive Types
let isAdmin: boolean = true; //boolean -> true / false
let name: string = "Luffy"; // String -> Collection of characters
let score: number = 98; // number -> interger or decimal number
let emptyValue: null = null; // null -> intentional absence of value
let notAssigned: undefined = undefined; // undefined -> variable declared but not assigned
const id = Symbol(); // Symbol -> unique value
const user = {
name: "Luffy",
[id]: 101,
};
console.log(user[id]); // Output: 101
Annotation: We have to give a data type in lower case to tell what kind of values will be store inside it. Example:- :number
Always use const or let to declare or initialize the variables .
Arrays
Arrays are a ordered collection of elements. In TS you can declare the type of array:-
let skills: string[] = [
"Web Development",
"UI/UX Designer",
"Backend Developement",
];
// string[] -> Array contains only strings
let marks: number[] = [70, 80, 90];
// number[] -> marks contains only numbers
// If you try:
skills.push(100); // Error: Number not allowed
// Other ways of declaring Arrays are:
const rating: Array<number> = [4.5, 3.5, 2.6];
// Multi-Dimensional Array
const table: number[][] = [
// 2D Array
[1, 2, 3],
[4, 5, 6],
];
// Tuple in TypeScript
// A tuple allows us to define an array with a fixed number of elements
// where each position has a specific type.
const shoeType: [string, number] = ["Bata", "8"];
const carType: [brand: string, modelNo: number] = ["BMW", "5"];
Type Safety in action.
Objects
Object are collection of item, which has key-value pairs.
// type -> defines a structure of object
type User = {
name: string; // name must be string
age: number; // age must be number
};
// object must follow the defined structure
const User: User = { name: "Zoro", age: 18 };
// Examples:
type Car = { modelName: string; modelYear: number; colors: string[] };
const car: Car = {
modelName: "Mustang",
modelYear: 1996,
colors: ["red", "yellow", "blue", "orange"],
};
const cars: Car[] = [
{
modelName: "Mustang",
modelYear: 1996,
colors: ["red", "yellow", "blue", "orange"],
},
{
modelName: "Mustang",
modelYear: 1996,
colors: ["red", "yellow", "blue", "orange"],
},
];
Type Inference
Relax - You don’t need to type everything.
TypeScript is smart.
let name = "Nami"; // TypeScript automatically infers -> string
const age = 17; // Automcatically infers -> number
const cars = ["BMW", "Mustang", "Audi"]; // infers into -> string[]
This is called Type Inference. It reduces typing effort and keeps code clean.
For simple variable use type inference.
Enums
Enum is a special type with represents a group of constants (unchangeable variables).
Enums let you define a fixed set of named values.
enum Directions {
North,
East,
South,
West,
}
let currentDirection = Directions.North;
console.log(currentDirection); // logs 0 because North is the first value
enum status {
pending = 100,
cancelled, // 101
success, // 102
} // the number automatically increases when give to one
console.log(status.success); // logs 102
enum ChaiType {
masala = "masala",
ginger = "ginger",
}
console.log(ChaiType.ginger); // logs ginger
In enums, technically, you can mix and match string and numeric enum values, but it is recommended not to do so.
Functions with Types
Functions are where TypeScript shines.
function add(a: number, b: number): number {
// a and b -> parameters are numbers
// :number -> return type of function
return a + b;
}
// If you return something wrong:
function add(a: number, b: number): number {
return "hello";
} // Error: Return type must be number
Now bugs are caught during development, not production.
Arrow Functions
// Example of Arrow Functions:
const product = (a: number, b: number): void => {
console.log(a * b);
}; // if a function returns nothing we can use :void
Optional & Default Properties
Real-world data’s are messy. Sometimes fields are optional.
Optional properties allow certain fields to be included or omitted when creating an object.
type User = {
name: string;
age?: number;
// ? -> optinal property
};
function add(a: number, b: number, c?: number): void {
console.log(a + b + c);
}
add(5, 3); // it is optinal to pass the 3rd variable
// Same way we can do it in objects, types and interfaces too
const person: User = {
name: "Luffy",
};
const person1: User = {
name: "Zoro",
age: 19,
};
// We even readonly keyword used for variables not to be modified & just to be read
type Car = {
modelName: string;
readonly modelYear: number;
};
const Car1: Car = {
modelName: "BMW",
modelYear: 1986, // throws error cause we cannot modify readonly values
};
Readonly properties can be accessed, but they cannot be reassigned or modified after initialization.
Union Types
Union Types are used when a value can be more than a single type.
Such as when a property would be string or number .
Union = OR
const getMonth = (month: string | number) => {
console.log(month);
};
getMonth(12); // takes number as input & logs 12
getMonth("December"); // takes string as input & logs December
function printStatusCode(code: string | number) {
console.log(`My status code is ${code.toUpperCase()}.`); // error: Property 'toUpperCase' does not exist on type 'string | number'. Property 'toUpperCase' does not exist on type 'number'
}
Very common in APIs where IDs might be numeric or string-based.
Interface vs Types
Beginners mostly confuse between interfaces and types at beginning.
In simple words:
They define what an object looks like.
So first let’s understand what interface and Types is:
Interface
Interfaces are similar to types, except that they only apply to object types.
interface User {
name: string; // must be string
age: number; // must be number
}
const user1: User = {
name: "Luffy",
age: 21,
};
// If you forget a property:
const user2: User = {
name: "Zoro",
// ❌ Error: age is missing
};
// We can use extends to inherit properties from a interface to another one.
interface Person {
name: string;
age: number;
}
interface Employee extends Person {
employeeId: number;
department: string;
}
The interface enforces structure.
Type
A type can also define object shapes.
type User = {
name: string;
age: number;
};
const user: User = {
name: "Rahul",
age: 22,
};
// So far, looks the same as interface.
// We can't use extends keyword but we use intersection &
type Person = {
name: string;
age: number;
};
type Employee = Person & {
employeeId: number;
department: string;
};
// & means combine both types.
Using Interface and types are almost same. They both apply on object’s.
Except that type can be used for creating unions, creating intersections and defining complex types.
And interface can be used while working with classes, building large application's, designing API’s.
Generics in TypeScript
What are generics
Generics allow you to write reusable, flexible types.
Instead of locking a function or type to one specific data type, generic allows it to adapt.
Think of it like: “A Placeholder for a type.”
We usually use T (but it can be any name).
Basic Generic Function
// Without generics:
function identity(value: string): string {
return value;
}
// Only used for strings.
// With generics:
function identity<T>(value: T): T {
// T = placeholder type
return value;
}
// now it works for anything.
identity<string>("Hello");
identity<number>(123);
identity<boolean>(true);
What happens?
If you pass string → T becomes string.
If you pass number → T becomes number.
Generic Arrays
function getFirstItem<T>(items: T[]): T {
return items[0];
}
// usage:
getFirstItem<number>([1, 2, 3]);
getFirstItem<string>(["a", "b", "c"]);
T[] means: An array of type T .
Generic with constraints (extends)
Sometimes you don’t want “anything”. You want “anything with some structure”.
function printLength<T extends { length: number }>(item: T): void {
console.log(item.length);
}
// Now T must have a length property.
// Valid:
printLength("Hello"); // string has length
printLength([1, 2, 3]); // array has length
// Invalid:
printLength(123);
// ❌ number does not have length
This is called generic constraint.
Utility Types (Built-in Generics)
Now we move to the powerful built-in helpers.
These are generic types provided by TypeScript.
Partial<T>
Makes all properties optional.
type User = {
id: number;
name: string;
email: string;
};
type UpdateUser = Partial<User>;
// Equivalent to:
type UpdateUser = {
id?: number;
name?: string;
email?: string;
};
// Why useful?
// When updating data:
const updateData: UpdateUser = {
name: "New Name",
};
You don't need to send full object.
Required<T>
Opposite of Partial.
Makes al properties required.
type OptionalUser = {
id?: number;
name?: string;
};
type StrictUser = Required<OptionalUser>;
// Now both properties are mandatory.
type User = {
id: number;
name: string;
email: string;
};
// Pick only 'id' and 'name' from the User type
type UserPreview = Pick<User, "id" | "name">;
// This object is valid because it contains exactly 'id' and 'name'
const preview: UserPreview = {
id: 1,
name: "Mohan",
};
// Error: Property 'email' does not exist on type 'UserPreview'
// preview.email = "mohan@example.com";
Useful for:
API responses.
Lightweight objects
Omit<T,K>
Remove certain properties. Opposite of Pick<T, K> .
type User = {
id: number;
name: string;
password: string;
};
// Creates a new type by removing "password" from User
type PublicUser = Omit<User, "password">;
// This is valid:
const userProfile: PublicUser = {
id: 101,
name: "Arjun",
};
// Error: Property 'password' does not exist on type 'PublicUser'
Very common in backend + frontend separation.
Record<T, K>
Creates an object with keys of specific type.
type UserRoles = Record<string, string>;
// Equivalent to:
type UserRoles = {
[key: string]: string;
};
// Example:
const roles: UserRoles = {
admin: "Full Access",
editor: "Edit Access",
};
Promise<T>
Promise is also generic.
It defines what type will be returned when resolved.
function fetchUser(): Promise<User> {
return fetch("/api/user").then((res) => res.json());
}
// Promise<User> means after async relsoves -> you get a User type.
// Example:
async function loadUser() {
const user = await fetchUser();
// user is strongly typed
}
If API change structure → TypeScript warns you.
Combining Utility Types
type User = {
id: number;
name: string;
email: string;
password: string;
};
type SafeUserUpdate = Partial<Omit<User, "password">>;
What this means:
Removes password
Makes remaining field optional
This is production-level typing.
TypeScript with React.
I know i haven’t thought react yet, but the next one would be about react, so just go through it or you can check it out when you are done learning react.
So the bug are stopped instantly instead in production.
Typing Function Props
Functions props are very common.
type UserCardProps = {
onSelect: (id: number) => void;
};
const UserCard = ({ onSelect }: UserCardProps) => {
return <button onClick={() => onSelect(1)}>Select</button>;
};
// Explaination of the above program
// - Accepts number
// - Returns Nothing
Typing useState
Simple Case(Type inference works)
const [count, setCount] = useState(0);
// inferred as number
// When you must Type it
// - Initial value is null
const [user, setUser] = useState<User | null>(null);
// Without typing, TS thinks it's only null
// - arrays
type User = {
id: number;
name: string;
};
const [users, setUsers] = useState<User[]>([]);
// now users is always an array of user.
Typing Events (React-Specific)
These types come from React — not plain TypeScript.
type User = {
id: number;
name: string;
};
const fetchUsers = async (): Promise<User[]> => {
const res = await fetch("/api/users");
return res.json();
};
Now your UI depends on strongly typed data.
If backend changes structure → you know immediately.
Why Generics Matter in React
In React:
useState<T>()
useRef<T>()
Promise<T>
Component generics
Utility types for props
All powered by generics.
Without understanding generics, TypeScript feels confusing.
With understanding generics, Everything clicks.
Common Patterns Professionals Use
Union props for variants
Extending HTML attributes
Generics for reusable components
Typing API responses
Using ReactNode for children.
Strict typing for forms
Common Beginner Mistakes
When starting with TypeScript, most developers struggle not because it’s hard — but because they approach it the wrong way.
Let’s fix that:
Typing Everything Manually
You don’t need to write types for every single variable.
const name: string = "Luffy";
This works — but it’s unnecessary.
const name = "Mohan";
// TypeScript already knows it's a string
Let inference work for you.
Fighting inference
Don’t manually type everything. Trust TypeScript unless necessary.
Ignoring errors
Red lines protect you. Don’t blindly use @ts-ignore.
Overcomplicating types
Avoid advanced patterns before mastering basics. Keep it simple.
Copying complex generics
If you don’t understand it, don’t use it.
🔴 Red lines are future bug prevention.
Practice regularly — that’s how TypeScript clicks.
What you can Ignore (For Now)
To avoid overwhelm, skip these early on:
Advanced generics
Complex utility combinations
Decorators
Namespaces
Deep tsconfig customization
You don’t need them to build real projects.
You will naturally encounter them when your projects grow.
For now:
Master the fundamentals.
Migration Mindset
One of the biggest misconceptions:
I need to rewrite my entire project in TypeScript.
No, you don’t.
Professional teams migrate gradually.
The Right Approach
Add TypeScript step by step
Rename .js files to .ts one by one
Type important modules first
Start with:
API response types
Forms
Reusable components
Shared utilities
Let the compiler guide improvements.
Each error it shows is an opportunity to strengthen your code.
Final Advice
TypeScript is not about strictness. It’s about:
Clarity — You know what data looks like.
Safety — Fewer runtime surprises.
Maintainability — Easier collaboration.
Confidence — Refactor without fear.
When you understand this shift, everything changes.
Strong mindset shift:
JavaScript lets you build.
TypeScript lets you build without fear.
What’s Next?
You now understand how TypeScript adds structure, safety, and clarity to your code.
You can define shapes, control data flow, prevent silent bugs, and build more predictable components. That’s a serious upgrade.
But knowing syntax isn’t enough.
In the next part, we’ll apply TypeScript inside real React applications — typing props properly, structuring API data, handling forms safely, and building scalable component patterns.