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 Assertion (টাইপ অ্যাসারশন)
TypeScriptIntermediate

Type Assertion (টাইপ অ্যাসারশন)

as, angle bracket, non-null assertion (!), satisfies এবং কখন assertion এড়িয়ে চলা ভালো — বাংলায়।

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

Type assertion দিয়ে আমরা TypeScript-কে বলি — এই মান সম্পর্কে আমাদের ধারণা তোমার চেয়ে ভালো, সরাসরি এটা নাও। এটি runtime-এ কিছু বদলায় না, শুধু compile-time-এ টাইপ বদলায়।

as সিনট্যাক্স

const input = document.getElementById("search") as HTMLInputElement;

input.value = "টেক্সট সার্চ";   // এখন value টাইপ-নিরাপদ

// JSON থেকে আনা data
const raw: unknown = '{"name":"রহিম"}';
const parsed = JSON.parse(raw as string);

Angle Bracket সিনট্যাক্স

আগেকার পদ্ধতিতে <> দিয়েও assertion লেখা যায়। তবে .tsx (React/TSX) ফাইলে এটি JSX-এর সাথে বিভ্রান্ত হয় — তাই as-ই প্রস্তাবিত।

// শুধু .ts ফাইলে
const total = <number>someValue;

// .tsx-এ একই কাজ
const total2 = someValue as number;

Non-null Assertion (!)

নিশ্চিত যে মান null/undefined নয়, সেটা বলতে ! ব্যবহার হয় — এটি assertion-এর একটি দ্রুত রূপ।

const container = document.querySelector("#app");
container!.innerHTML = "<p>লোড হয়েছে</p>";

function findName(list: string[], key: string): string | undefined {
  return list.find((item) => item === key);
}

const found = findName(["রহিম", "করিম"], "রহিম")!;
console.log(found.toUpperCase());

satisfies কীওয়ার্ড

satisfies মানে — টাইপটি মেন চলছে তো? — কিন্তু টাইপ বদলায় না। constraining-এর সাথে exact narrowing-এর সুবিধা দুটোই মেলে।

type Config = { ratio: number | string };

const config = {
  ratio: 0.5,
} satisfies Config;

// ratio-এর টাইপ: number (ন্যারো) — string-এ রূপান্তর হয়নি
const double = config.ratio * 2;   // ✅ number

কখন assertion risk?

  • টাইপ বিভ্রান্ত হলে assertion চায় না — আগে ঠিক টাইপ দাও
  • runtime-এ মান সম্পূর্ণ আলাদা হলে assertion মিথ্যা হবে
  • as any এরপর as Type — double assertion হল বুঝে নাও বিশ্বাসের ঘাটতি আছে
  • বাস্তবে structure যাচাই করতে generic + type guard ভালো
সতর্কতা

Assertion compile-time-এ টাইপ ভুল ঢেকে রাখে, runtime-এ করে না। API ডেটার মতো অজানা মানে অযথা assertion-এর বদলে narrowing বা type guard ব্যবহার করুন।

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

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

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

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

mdanamolhasan1998@gmail.com