উদাহরণসহ TypeScript এর Optional Chaining Operator

  TypeScript Optional Chaining Operator with Example প্রিয় ডেভেলপারস ! আসসালামু আলাইকুম , আশা করি ভালো আছেন । TypeScript Operators সাধারণ...

 TypeScript Optional Chaining Operator with Example

TypeScript Optional Chaining Operator with Example

প্রিয় ডেভেলপারস! আসসালামু আলাইকুম, আশা করি ভালো আছেন TypeScript Operators সাধারণত JavaScript Operators এর মতোই কিন্তু TypeScript ডাটার স্ট্যাটিক টাইপ প্রদান করে থাকে যার ফলে কোডিং Error হওয়ার সম্ভাবনা অনেকাংশ কমে যায় নিম্নে TypeScript Operators এর Optional Chaining Operator উদাহরণসহ বর্ণনা করা হলো -

 

লক্ষণীয় যে, TypeScript Operators সম্পর্কে বিস্তারিত জানুন এখানে।

 

Optional Chaining Operator

? - Optional Chaining Operator সাধারণত ব্যবহার করা হয় কোন Variable এবং Object এর মধ্যে value আছে কি বা নাই তা জানার জন্য এবং কোড নিরাপদে রান করাতে সাহায্য করে

[

// Define a simple object with nested properties

interface Person {

  name: string;

  age?: number;

  address?: {

    city?: string;

    zipCode?: string;

  };

}


// Create an instance of the Person interface

const person: Person = {

  name: 'John Doe',

  // age is intentionally omitted

  // address is intentionally omitted

};


// Using Optional Chaining to safely access nested properties

const cityName = person.address?.city;

const zipCode = person.address?.zipCode;


// Accessing a property that may not exist

const personAge = person.age?.toFixed(2);


// Display the results

console.log('City Name:', cityName); // Output: City Name: undefined

console.log('Zip Code:', zipCode);   // Output: Zip Code: undefined

console.log('Person Age:', personAge); // Output: Person Age: undefined

]


[

interface Order {

  orderId: number;

  customer?: {

    name?: string;

    contact?: {

      email?: string;

      phone?: string;

    };

  };

  products?: {

    id: number;

    name: string;

    price?: number;

  }[];

}


// Create an instance of the Order interface

const order: Order = {

  orderId: 123,

  // customer is intentionally omitted

  // products is intentionally omitted

};


// Using Optional Chaining to safely access nested properties

const customerName = order.customer?.name;

const customerEmail = order.customer?.contact?.email;

const productName = order.products?.[0]?.name;

const productPrice = order.products?.[0]?.price;


// Display the results

console.log('Customer Name:', customerName);      // Output: Customer Name: undefined

console.log('Customer Email:', customerEmail);    // Output: Customer Email: undefined

console.log('Product Name:', productName);        // Output: Product Name: undefined

console.log('Product Price:', productPrice);      // Output: Product Price: undefined

]


[

interface Car {

  model: string;

  manufacturer?: {

    name?: string;

    address?: {

      city?: string;

      country?: string;

    };

  };

  startEngine?(): void;

}


// Create an instance of the Car interface

const myCar: Car = {

  model: 'XYZ',

  // manufacturer is intentionally omitted

};


// Using Optional Chaining to safely call a method and access nested properties

const manufacturerName = myCar.manufacturer?.name;

const manufacturerCity = myCar.manufacturer?.address?.city;


// Using Optional Chaining to safely call a method that might not exist

myCar.startEngine?.(); // No error even if startEngine is undefined


// Display the results

console.log('Manufacturer Name:', manufacturerName);  // Output: Manufacturer Name: undefined

console.log('Manufacturer City:', manufacturerCity);  // Output: Manufacturer City: undefined


]

 

 

আশা করি আপনি আজকের এই ব্লগটি মনোযোগ সহকারে পড়েছেন আজকের ব্লগটি আপনার কাছে কেমন লেগেছে অবশ্যই  কমেন্ট করে জানাবেন, আর যদি কোন ভূল হয়ে থাকে তাহলে ক্ষমা সুন্দর দৃষ্টিতে দেখবেন এবং কোথায় ভূল হয়েছে কমেন্ট করে জানাবেনপোষ্টে উল্লিখিত কোনো অংশ যদি বুঝতে সমস্যা হয়, তাহলে কমেন্ট বক্সে জানিয়ে দিনইনশাআল্লাহ্‌, সমস্যা সমাধানের চেষ্টা করবো আর ওয়েবসাইটটি বন্ধুদের মাঝে শেয়ার করবেন আজকের মতই এখানেই বিদায় নিলাম, ইনশাআল্লাহ দেখা হবে অন্য কোন ব্লগে ভালো থাকবেন সুস্থ থাকবেন আল্লাহ হাফেয

COMMENTS

Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content