Build a mini Typed API Wrapper using native fetch. The goal was to ensure that all data coming from a public API was strictly typed before it reached the frontend. Core concepts I learned: Passing Generic Functions: I built a 'handleData<T>' function that takes the URL and the generic fetcher function as arguments '(datafetcher: (url: string) => Promise<T[]>)'. This keeps the data-handling logic highly modular. Generic Scoping: I learned the difference between an outer generic and an inner generic. By declaring <T> on the outer wrapper function '(handleData<T>)', it locks the type in immediately. This forces any inner callback functions to strictly adopt that type, making the logic highly predictable. Parameter Contracts: I learned why we strictly type function arguments like 'datafetcher: () => Promise<T[]>'. It acts as a strict contract (or a bouncer). It does not tell the fetcher what to do; instead, it protects the main function from accidentally accepting a callback that returns the wrong kind of data. The Async/Await Illusion: A major realization was that await does not make code synchronous. It simply pauses that specific function and yields control back to the main thread so the browser UI does not freeze. Because the function pauses, the async keyword automatically and synchronously returns a Promise (an IOU) to keep the rest of the application running smoothly. #TypeScript #AsyncProgramming #JavaScript #WebDev #LearningInPublic #StudentDeveloper #APIs

To view or add a comment, sign in

Explore content categories