If you work with TypeScript you may be familiar with “utility types”, which take a given type and transform it somehow into a different type. It’s a function which takes a type as an input and modifies it and returns a new type. TypeScript has many built-in utility types, which are detailed here: TypeScript’s introductory article on utility types
The built-in utility types are great, but you can also make your own. Here’s one we’ve made which is extra special:
This is a utility type which takes anything you throw at it, objects, arrays, primitives, you name it, and converts Date values to string. It’ll then be strings-for-dates all the way down
We use RecursivelyConvertDatesToStrings to transform the typings of our endpoint responses (typically ORM models arranged and prepped in one form or another) from how they look server-side, into how they arrive client-side, upon a fetch operation. Because our endpoints return data via JSON in the standard way, there’s no way for any Date fields to arrive in-tact. They’re necessarily flattened into strings for the HTTP response.
So, we type our “fetched endpoint responses”, by taking our server-side “endpoint responses” and running them through this utility type. That way TypeScript knows that all our favorite date fields, such as createdAt or updatedAt are now of string type, and our client-side state and rendering code set up for success.
Get to know what Basedash can do and how it changes traditional internal tools.
See a full app that connects to a Postgres database and external API made from scratch.
Ship your product faster.
Worry about internal tools less.
No credit card required.
January 20, 2023
We’ve previously explained what admin panels do; in this post we’ll talk about the most common problems they actually solve.
January 11, 2023
Learn about how to unify related datasets using SQL when a Foreign Key is and isn't present in the tables you're looking to unify.
January 3, 2023
This post is the first part of a series talking about my experience applying to and taking part in the Sumer 2020 batch of Y Combinator with my company, Basedash.
December 16, 2022
How to create a MariaDB database on DigitalOcean and connect to it via a remote server so you can access your data in Basedash.
December 9, 2022
A startup's primary focus is to build a foundation of core components that are resilient, adaptable, and can be combined to create an innovative and useful product, according to a blog post by Basedash. This is often misunderstood as being about creating a product people love.