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. Generics (জেনেরিক)
TypeScriptIntermediate

Generics (জেনেরিক)

Generic function, type parameter, multiple types, constraints (extends), generic interface ও default parameter — বাংলায়।

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

Generics দিয়ে এমন ফাংশন/টাইপ বানানো যায় যা টাইপটি ব্যবহারকারী অনুযায়ী ঠিক করে নেয় — একটি কোড, অনেক টাইপের জন্য নিরাপদ।

Generic Function

// T হলো type parameter — call-এর সময় টাইপ ঠিক হয়
function identity<T>(value: T): T {
  return value;
}

const a = identity("রহিম");   // T = string
const b = identity(42);       // T = number

// বা স্পষ্টভাবে নির্ধারণ
const c = identity<boolean>(true);

Array এবং .map দিয়ে

function firstElement<T>(arr: T[]): T | undefined {
  return arr[0];
}

const first = firstElement([10, 20, 30]);    // number | undefined
const hello = firstElement(["নমস্কার"]);     // string | undefined

Multiple Types

function merge<A, B>(first: A, second: B): A & B {
  return { ...first, ...second };
}

const merged = merge({ name: "রহিম" }, { age: 28 });
// টাইপ: { name: string } & { age: number }

Constraints (extends)

extends দিয়ে বাধা দেওয়া যায় parameter টির নির্দিষ্ট গুণ থাকতে হবে — ফলে .length ইত্যাদি ব্যবহার নিরাপদ হয়।

interface HasLength {
  length: number;
}

function longest<A extends HasLength, B extends HasLength>(
  x: A,
  y: B,
): A {
  return x.length >= y.length ? x : y;
}

console.log(longest("বাংলাদেশ", "ভারত"));    // "বাংলাদেশ"

Generic Interface ও Type

interface ApiResponse<T> {
  data: T;
  status: number;
}

type User = { name: string; id: number };

const response: ApiResponse<User> = {
  status: 200,
  data: { name: "রহিম", id: 1 },
};

// টাইপের ভেতরেও generic
type TList<T> = { items: T[]; count: number };

Default Type Parameter

function createStore<T = string>() {
  return { values: [] as T[] };
}

const store = createStore();          // T = string (default)
const numStore = createStore<number>();  // T = number
কখন generics?

যখন একটি ফাংশন/কম্পোনেন্ট বিভিন্ন টাইপের সাথে একভাবে কাজ করবে, তখন generics ইনপুট-আউটপুট সম্পর্ক ধরে রাখে — any-এর থেকে অনেক নিরাপদ।

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

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

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

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

mdanamolhasan1998@gmail.com