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 Guard (টাইপ গার্ড)
TypeScriptIntermediate

Type Guard (টাইপ গার্ড)

User-defined type guard — নিজের function দিয়ে টাইপ সরু করা, `is` operator ও কাস্টম গার্ড — বাংলায়।

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

Type guard হলো এমন একটি অভিব্যক্তি যা টাইপ constraining করে। আগের অধ্যায়ের typeof, in, instanceof — সবই built-in guard। চাইলে নিজের custom guard-ও বানানো যায়।

User-defined Type Guard

একটি ফাংশনের return type-এ value is Type লিখলে TypeScript বোঝে — ফাংশন true ফিরলে টাইপ সেই নির্দিষ্ট টাইপে সরু হয়ে যাবে।

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

function isFish(pet: Fish | Bird): pet is Fish {
  return (pet as Fish).swim !== undefined;
}

const pets: (Fish | Bird)[] = [
  { name: "কই", swim: () => console.log("সাঁতার") },
  { name: "টিয়া", fly: () => console.log("উড়া") },
];

const fishes = pets.filter(isFish);   // টাইপ: Fish[]

কার্যকরী ব্যবহার

function feed(pet: Fish | Bird): void {
  if (isFish(pet)) {
    pet.swim();    // pet: Fish
  } else {
    pet.fly();     // pet: Bird
  }
}

// Array filtering-এও টাইপ বজায় থাকে guard-এর কারণে
const names = fishes.map((fish) => fish.name);   // string[]

Discriminated Union guard

kind ট্যাগ-ভিত্তিক union-এ প্রতিটি member আলাদা হলে, একটি ছোট guard function দিয়ে পুরো টাইপ নিরাপদ করা যায়।

type Video = { type: "video"; duration: number };
type Article = { type: "article"; wordCount: number };

const isVideo = (item: Video | Article): item is Video =>
  item.type === "video";

function preview(item: Video | Article): string {
  return isVideo(item)
    ? item.duration + " সেকেন্ড"
    : item.wordCount + " শব্দ";
}
গার্ডের সততা

guard function-টির ভেতরের বাস্তব পরীক্ষা অবশ্যই return-type-এর সাথে মিলতে হবে — নাহলে runtime ও compile-time দুই জায়গাতেই বিভ্রান্তি তৈরি হবে।

শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।
আগেরType Narrowing (টাইপ ন্যারোয়িং)পরেরEnums (এনাম)
এই পেজে
Tech Tutor

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

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

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

mdanamolhasan1998@gmail.com