A comprehensive developer's guide to TypeScript. Interfaces, union/intersection types, generics, conditional utility types, compiler hardening, and code standards.
strictNullChecks and disable the any keyword to ensure maximum type safety.infer keyword to extract signatures from arguments.TypeScript provides compile-time safety by typing variables explicitly or through inference.
// Unions and Intersections
type Status = "idle" | "loading" | "success" | "error";
interface User {
id: string;
name: string;
}
interface Admin {
permissions: string[];
}
// Intersection type
type AdminUser = User & Admin;
const admin: AdminUser = {
id: "001",
name: "Ajit Dev",
permissions: ["write_blogs", "deploy_infrastructure"]
};
TypeScript matches types based on their shape (structural typing) rather than explicit names.
interface Point {
x: number;
y: number;
}
// Can be extended via declaration merging
interface Point {
z?: number; // Optional property
}
Generics act as type parameters, allowing functions or components to adapt to different datasets while preserving type details.
// Generic API Response wrapper
interface ApiResponse<T> {
data: T;
status: number;
message: string;
}
interface BlogPost {
title: string;
slug: string;
}
function processResponse<T>(res: ApiResponse<T>): T {
return res.data;
}
TypeScript provides built-in helper types to transform existing structures:
Partial<T>: Makes all properties in T optional.Required<T>: Makes all properties in T required.Readonly<T>: Makes all properties in T readonly.Record<K, T>: Constructs an object type with keys K and values T.Pick<T, K>: Constructs a type by picking a subset of keys K from T.Omit<T, K>: Constructs a type by omitting keys K from T.interface Article {
id: string;
title: string;
content: string;
}
// Pick title and content only
type ArticlePreview = Pick<Article, "title" | "content">;
// Omit the ID field
type AnonymousArticle = Omit<Article, "id">;
Conditional types choose types dynamically based on generic relationships:
type IsString<T> = T extends string ? true : false;
type A = IsString<string>; // true
type B = IsString<number>; // false
infer keywordExtract nested type information within conditional tests:
type UnpackArray<T> = T extends (infer U)[] ? U : T;
type ElementType = UnpackArray<number[]>; // number
To prevent runtime errors, configure your tsconfig.json with strict rules:
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"exactOptionalPropertyTypes": true
}
}
as) vs type casting?
unknown type represent?
unknown is the type-safe counterpart of any. Any value can be assigned to unknown, but you cannot perform operations on it without type narrowing.kind or type), which is used to narrow down the union type dynamically.CTO @ Brainzima Innovation Institute 🚀 · Full Stack Developer & DevOps Engineer
Ajit Dev (also known as Ajit Kumar) is based in Katihar, Bihar, India. Specializing in MERN Stack, Next.js, Cloud Security, and Distributed Systems.