Our great sponsors
-
SurveyJS
Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
ws.on("message", (data) => { try { const parsed = EventSchema.parse(JSON.parse(data)); // Parse will throw for invalid events // ^ // `--- CreatePost | UpdatePost switch (parsed.type) { case "CreatePost": ws.send(handleCreatePost(parsed)) // ^ // `---- type of parsed is CreatePost in this branch return; case "UpdatePost": ws.send(handleUpdatePost(parsed)) // ^ // `---- type of parsed is UpdatePost in this branch return; } } catch (e) { console.error(e); // Also see https://github.com/colinhacks/zod#error-formatting } })
In many functional languages, we have support for pattern-matching with built in support for exhaustiveness check. Typescript does not have this at the moment but there are userland implementations that emulate pattern matching. One such library is ts-pattern, which I have found to work very well in practice.
Related posts
- Write fewer tests by creating better TypeScript types
- TypeScript Essentials: Distinguishing Types with Branding
- You can’t run away from runtime errors using TypeScript
- Epic Next JS 14 Tutorial Part 4: How To Handle Login And Authentication in Next.js
- Error handling in our form component for the NextAuth CredentialsProvider