ryer.io

Resolving Data Fetch and TypeScript Challenges

TL;DR

  • Ensured fetching logic for random content tiles aligns with expected outcomes.
  • Identified and resolved an issue with index-based fetching causing unexpected results.
  • Avoided using TypeScript’s as and satisfies for type casting by restructuring data.
  • Highlighted backend vs frontend data needs, simplifying type handling.

Yesterday, I tackled the challenge of fixing the logic for fetching random content tiles to ensure they always conform to the expected structure. While the initial logic handled prescribed and random tiles well, issues arose with the randomness of tile fetching. This required troubleshooting the logic.

Steps Taken:

  1. Problem Identification:

    • Logic was supposed to fetch a quote and learn tile randomly when no prescribed content was present.
    • On day two, realized the fetching wasn’t producing random results as expected.
  2. Debugging Approach:

    • Hypothesized the issue was linked to the way content was sorted and indexed upon fetching.
    • Used createdAt and ID for consistent ordering but suspected this wasn’t enough.
  3. Testing and Adjustment:

    • Realized direct access via an index might not fetch accurate data.
    • Decided to retrieve sorted data, then access by index in JavaScript.
  4. TypeScript Type Error Resolution:

    • Encountered type errors while integrating back-end processing and front-end expectations.
    • Recognized disconnect between backend data processing and frontend data consumption.
    • Opted to create a streamlined data structure (returnable content) to bridge this while respecting TypeScript’s type safety.

Through these adjustments, I enhanced both backend processing and frontend data delivery, achieving randomness in tile fetching without compromising type integrity.