JavaScriptIntermediate

TypeScript Essentials

Add static types to JavaScript with TypeScript - interfaces, generics, and type safety for larger codebases.

5 hours
5 key concepts
2 in-depth sections
Official Docs

0 of 5 concepts completed (0%)

What You'll Learn

1.Types & Interfaces

typescript
// Interface for object shapes, extendable
interface User {
  readonly id: string;
  name: string;
  email: string;
  age?: number;  // optional
  address: { city: string; country: string };

interface AdminUser extends User { role: "admin"; permissions: string[]; }

// Type alias for unions, intersections, primitives type ID = string | number; type Status = "active" | "inactive" | "pending"; type AdminUser = User & { role: "admin" }; // intersection

// Generics function first(arr: T[]): T | undefined { return arr[0]; }

interface Repository { findById(id: string): Promise; create(data: Omit): Promise; update(id: string, data: Partial): Promise; }

// Utility types type UserPreview = Pick; type UpdateUser = Partial>; type ReadonlyUser = Readonly; ```

2.Discriminated Unions & Advanced Types

typescript
// Discriminated unions exhaustive type narrowing
type Shape =
  | { kind: "circle"; radius: number }
  | { kind: "rectangle"; width: number; height: number }

function area(shape: Shape): number { switch (shape.kind) { case "circle": return Math.PI * shape.radius ** 2; case "rectangle": return shape.width * shape.height; case "triangle": return 0.5 * shape.base * shape.height; // TypeScript errors if any case is missing } }

// Template literal types type EventName = on${Capitalize}; type CSSUnit = ${number}${"px" | "rem" | "em" | "%"};

// Conditional types type NonNullable = T extends null | undefined ? never : T; type Awaited = T extends Promise ? Awaited : T;

// Mapped types type Optional = { [K in keyof T]?: T[K] }; type Flags = { [K in keyof T]: boolean }; ```

Code Example

// Interfaces
interface User {
  id: string;
  name: string;
  age?: number;
}

// Generics
function identity<T>(value: T): T {
  return value;
}

// Union types
type Status = "active" | "inactive" | "pending";

// React with TypeScript
interface ButtonProps {
  label: string;
  onClick: () => void;
  disabled?: boolean;
}

const Button: React.FC<ButtonProps> = ({ label, onClick }) => (
  <button onClick={onClick}>{label}</button>
);

Pro Tips

  • 1Prefer 'unknown' over 'any'. Unknown requires type narrowing before use it's type-safe. 'any' defeats TypeScript's purpose.
  • 2Use discriminated unions for state machines: type State = {status:'loading'} | {status:'error',error:string} | {status:'success',data:User}
  • 3Enable 'strict: true' in tsconfig.json from the start. Turning it on later creates hundreds of errors in existing codebases.
  • 4Utility types (Partial<T>, Pick<T,K>, Omit<T,K>, Record<K,V>) eliminate most manual type definitions. Learn them first.

Topics Covered

TypeScriptTypesGenerics

Continue Learning

Explore the official documentation and deepen your knowledge with real-world practice.

Explore the Full Portfolio

This is the complete portfolio of Sufi Khan Sulaiman, a technology leader specialising in B2B commerce and digital automation. Start from the Home page for the overview, then move through two decades of career experience across FLIR Systems, Lorex Technology, and 1c Platform, and the full catalogue of project case studies spanning headless commerce migrations, AI recommendation engines, and multi-channel fulfilment systems.

The skills and certifications page maps the technical and leadership capabilities behind the work, while the articles and the knowledge base break down the thinking into actionable frameworks. For hands-on learning, the tutorials and applications sections cover practical builds from front-end fundamentals to full-stack web apps.

For consulting engagement, the expertise page outlines service offerings, the ecommerce hub covers platform architecture and automation strategy, and the ecommerce guide (PDF) is a downloadable 55-page field manual. When you are ready to talk, the contact page is the direct line.