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. Conditional Types (শর্তাধীন টাইপ)
TypeScriptAdvanced

Conditional Types (শর্তাধীন টাইপ)

T extends U ? X : Y, infer keyword ও distributive behavior — Dynamic টাইপ রচনার প্রথম পাঠ।

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

Conditional type হলো টাইপ-স্তরের শর্ত — একটি টাইপ অন্য টাইপকে প্রসারিত করে (extends) কিনা দেখে নতুন টাইপ বানায়।

মৌলিক গঠন

// T extends U ? X : Y
type Wrapper<T> = T extends string ? "string-type" : "other-type";

type A = Wrapper<string>;     // "string-type"
type B = Wrapper<number>;     // "other-type"

// Custom utility — নিজের IsArray
type IsArray<T> = T extends unknown[] ? true : false;

type C = IsArray<string[]>;   // true
type D = IsArray<string>;     // false

Distributive Behavior

Conditional টাইপ union-এর প্রতিটি member-এ আলাদাভাবে প্রযোজ্য হয় — এর ফল union মিলিত থাকে। এতে Exclude-এর নিজস্ব তুলনা লেখা যায়।

type ToArray<T> = T extends unknown ? T[] : never;

// union distributive হয় — প্রতিটি member আলাদা
type Result = ToArray<string | number>;
// → string[] | number[]  (হয় string[], না হয় number[])

infer Keyword

infer দিয়ে conditional-এর মধ্যে থেকে TypeScript-কে আউটপুট টাইপ নিজেই বের করতে বলি — যেমন ফাংশনের return type বের করা।

type Unwrap<T> = T extends Promise<infer U> ? U : T;

type A = Unwrap<Promise<string>>;   // string
type B = Unwrap<number>;            // number

// ফাংশনের return type বের করা
type Return<T> = T extends (...args: any[]) => infer R ? R : never;

const g = () => "হ্যালো";
type RT = Return<typeof g>;    // string

Real-world প্রয়োগ

type GetData<T> = T extends { data: infer D } ? D : never;

interface ProductResponse {
  data: Product[];
  total: number;
}

interface Product {
  id: number;
  title: string;
}

type ProductData = GetData<ProductResponse>;   // Product[]
ধাপে ধাপে চিন্তা

Conditional type লেখার সময় ছোট ছোট টাইপে ভাগ করে test করুন — সাহসী জটিল টাইপের আগে প্রতিটি ধাপ নিশ্চিত করুন। infer জাগানো টাইপ বাছাইয়ের শক্তিশালী হাতিয়ার।

শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।
আগেরAsync ও Promise (টাইপ সহ)পরেরMapped Types (ম্যাপড টাইপ)
এই পেজে
Tech Tutor

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

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

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

mdanamolhasan1998@gmail.com