Tech TutorBangla Web Documentation
Portfolio
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • TypeScript Documentation28টি অধ্যায়
প্রযুক্তি নির্বাচন
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • Introduction
  • Installation
  • Basic Types
  • Variables & Inference
  • Functions
  • Objects
  • Type Alias
  • Interface
  • Union & Intersection
  • Type Narrowing
  • Type Guards
  • Enums
  • Generics
  • Classes
  • Utility Types
  • Type Assertion
  • Async TypeScript
  • Conditional Types
  • Mapped Types
  • Template Literal Types
  • tsconfig Options
  • Modules & Types
  • Decorators
  • React + TypeScript
  • Node.js + TypeScript
  • JS → TypeScript
  • Common Errors
  • Mini Project
  1. Docs
  2. TypeScript
  3. Type Narrowing (টাইপ ন্যারোয়িং)
TypeScriptIntermediate

Type Narrowing (টাইপ ন্যারোয়িং)

typeof, truthiness, equality, in, instanceof ও discriminated union — কীভাবে union টাইপকে সরু করে নিরাপদ কোড লেখা যায়।

15টি অংশবাংলা ভাষায়

Type narrowing মানে union টাইপের মান নিয়ে যাচাই করে টাইপটিকে সরু করা। condition-এর ভেতরে টাইপ আরও সুনির্দিষ্ট হয়ে যায়, ফলে প্রপার্টি নিরাপদে ব্যবহার করা যায়।

typeof narrowing

function printValue(value: string | number): void {
  if (typeof value === "string") {
    console.log("টেক্সট:", value.toUpperCase());
  } else {
    console.log("সংখ্যা:", value.toFixed(2));
  }
}

printValue("hello");
printValue(3.1416);

Truthiness narrowing

function getLength(text?: string): number {
  if (text) {
    return text.length;    // এখানে text: string
  }
  return 0;
}

Equality narrowing

function pick(x: string | number, y: string | boolean) {
  if (x === y) {
    // এখানে x ও y দুইটিই string
    console.log(x.toUpperCase());
  }
}

in Operator

type Fish = { swim: () => void };
type Bird = { fly: () => void };

function move(animal: Fish | Bird): void {
  if ("swim" in animal) {
    animal.swim();    // narrowing: Fish
  } else {
    animal.fly();     // narrowing: Bird
  }
}

instanceof Operator

function addOrLog(value: string[] | Date): void {
  if (value instanceof Date) {
    console.log(value.toISOString());
  } else {
    console.log(value.join(", "));
  }
}

Discriminated Unions

যখন প্রতিটি union member-এ একটি আলাদা tag প্রপার্টি থাকে (যেমন kind), তখন সেই ট্যাগ দিয়ে টাইপ সরু করা যায় — এটাই real-world-এর সবচেয়ে দরকারি প্যাটার্ন।

type Shape =
  | { kind: "circle"; radius: number }
  | { kind: "square"; size: number }
  | { kind: "rect"; width: number; height: number };

function area(shape: Shape): number {
  switch (shape.kind) {
    case "circle":
      return Math.PI * shape.radius ** 2;
    case "square":
      return shape.size ** 2;
    case "rect":
      return shape.width * shape.height;
  }
}

console.log(area({ kind: "circle", radius: 3 }));   // ≈ 28.27
exhaustive চেক

switch-এ নতুন case যোগ করতে ভুল হলে default-এ assertNever(value) ব্যবহার করুন — TypeScript তখন compile-error দেবে, যা পরিবর্তন tracking-এ অভিজ্ঞভাবে সাহায্য করে।

শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।
আগেরUnion ও Intersection TypesপরেরType Guard (টাইপ গার্ড)
এই পেজে
Tech Tutor

বাংলায় ওয়েব ডেভেলপমেন্ট ডকুমেন্টেশন শিখুন — ধাপে ধাপে।

Homeসব ডকুমেন্টেশনQuick Startআমার নোটস

© 2026 Tech Tutor. বাংলায় শিখুন, বাংলায় গড়ুন।

mdanamolhasan1998@gmail.com