In Typescript, exceptions can be a bit weird. This is especially true if you’re writing library code or a VSCode plugin. Because almost surely you’ll want to output nice and custom error messages to the end user. Typescript doesn’t allow for a try/catch block to catch type errors, like it would for runtime errors. So […]
Author: Martin Aarhus Gregersen
Switching from JS to TS as an experienced programmer
Few programmers start their journey with Typescript. There’s a wide variety of languages we could start with and while it isn’t all that important, a common denominator despite how we learn the web, is Javascript. Most developers encounter javascript at some point or another, as part of making their websites interactive, adding a little bit […]
3 libraries for how to best setup Typescript compilation with Javascript rerendering
This post was updated in May 2024, to include a new contender with fewer tools as a potential winner as my personal preference. For Javascript, we are lucky to have an extremely popular library titled nodemon. For Typescript, we’re not quite as lucky. But we do have a few options! In this short article, I’ll […]
How to write a function with a conditionally required parameter
Functions are the bread and butter of Javascript. It leans more strongly into a functional programming paradigm than any modern language today. But functions can be weird! Then, on top of that, let’s add the added layer of complexity that is Typescript. How do we start defining types for weird functions? What if I wanted […]
What’s a good use case for using Partial?
The Partial keyword in Typescript has many uses, but when is actually a good use case for using it? Well I’ve got a few quick ones so let’s dive in! Extending types to reduce maintenance Familiar with the DTO pattern? No? If not, this section is definitely for you! Popularized by Java – at least […]
Why ‘satisfies’ is a BIG deal
With the most recent Typescript version as of today – 4.9 – among a bunch of features (that I’ll make sure to cover in other posts) we are getting access to the satisfies keyword and that is a big deal! Bigger than you think, at least. At its core level the purpose of the satisfies […]
I used to hate Typescript. Here’s why I love it now
While Typescript has a high rate of adoption and it is generally very well-regarded in the community, there are still many developers that – despite Typescripts many advantages – still prefer Javascript. I myself had a preference for Javascript well into 2021, before I finally succumbed to the allures of Typescript, and decided to give […]
Understanding ‘this’ in Builder classes
Builder classes are an interesting pattern. Originating from the OOP world, they seem to have found a good place in the Javascript (and Typescript) ecosystem. Builders can be incredibly helpful in assisting us in creating complex objects, often step-by-step. They are at their best in code-bases where we often need similar, yet different, complex objects […]
How to tell Typescript a function always returns something
Nothing is more frustrating than having the Typescript compiler complain about something that obviously worked fine in Javascript, what the hell man!? However, as frustrating as it seems, Typescript is actually trying to protect us from… Well, ourselves! Sometimes we know more than Typescript and we can safely ignore such warnings, for example by declaring […]
Type safety and generics do not have to be complicated!
Ever find yourself consuming a third party library, or API, with Typescript? Ever been in a situation where whatever you’re working with, isn’t exactly strongly typed, entirely neglecting one of the advantages of Typescript? Me too! But fret not, there are things we can do to ensure type safety even when working with libraries, objects […]