errors(2/0)
Conditional and union types are better
Elsewhere I advocate for that developers use http response objects. TypeScript has a couple nifty improvements that can really make these response objects simple: Union and Conditional types. Let’s say you have an Author object returned from your API. Now you want to add response properties. Type Union to the rescue! export interface Response { success: boolean; message?: string; } export interface Author { id: number; name: string; age: number; } // the union is an ampersand type AuthorResponse = Author & Response; // and a typed example const x = <AuthorResponse>{ success: true, name: 'Alex', age: 34 }; That worked great for your /artist GET request, but now you need an /artist PUT.…
Use HTTP response objects
As Victoria mentions in her post, the type of code you’re writing affects how you’ll handle errors. Her example matches code that integrates with other systems on the same machine (and probably other cases). The code I’m writing these days, when it’s not JavaScript, is for back-end services. I’ve discovered that services delivered over HTTP/S are best implemented to capture exceptions in the service and return response objects. I’ll usually do something like this:…