Handling "Property 'description' is Possibly 'Undefined'" in TypeScript

I was building a dynamic category page in Next.js where blog posts are displayed. Each post includes a title, image, categories, and a short description. However, when accessing the description property of a post object, I encountered the TypeScript error:
'post.description' is possibly 'undefined'.

I realized this happened because TypeScript inferred that the description property might not always be defined for all post objects in the posts array. TypeScript, being strict about undefined properties, flagged this as a potential runtime issue.

How I Fixed It

I resolved this issue by implementing a fallback mechanism for the description property. Here’s how I did it:

  1. Understand the Source of the Error:
    I examined the posts array structure and confirmed that not all post objects guaranteed the presence of a description field.

  2. Use the Nullish Coalescing Operator (??):
    To provide a fallback value in case description is undefined or null, I updated the code as follows:

    <h3 className="text-base text-muted-foreground">
      <span>
        {post.description?.length > 90
          ? `${post.description.slice(0, 90)}...`
          : post.description ?? "No description available"}
      </span>
    </h3>
    
    • The ?. (optional chaining) ensures the length check is only performed if description is defined.
    • The ?? (nullish coalescing) operator provides a default string if description is undefined or null.
  3. Optional: Use a Type Guard:
    If preferred, I could explicitly check for the existence of the description property before attempting to access or manipulate it:

    <h3 className="text-base text-muted-foreground">
      <span>
        {post.description
          ? post.description.length > 90
            ? `${post.description.slice(0, 90)}...`
            : post.description
          : "No description available"}
      </span>
    </h3>
    
  4. Test the Solution:
    After implementing the fallback mechanism, I ran the application and verified that:

    • Posts with a defined description displayed correctly.
    • Posts without a description showed the fallback text: “No description available.”

By following these steps, I ensured that the code handled undefined description values gracefully without triggering TypeScript errors or runtime crashes.