Programming

Avoid Expensive innerHTML Manipulation with insertAdjacentHTML

Posted on

It is pretty well known these days that fiddling with innerHTML is terribly inefficient. It triggers serialization, and it can result in invalid markup. Nevertheless, you’ll still find this being done in many applications. elem.innerHTML = elem.innerHTML + ‘<a href=”https://www.example.com”>Visit Example Dot Com</a>’; // or elem.innerHTML += ‘<a href=”https://www.example.com”>Visit Example Dot Com</a>’; You can avoid […]

Programming

The ESM Module Loader is Experimental

Posted on

If you fire up a new TypeScript app and whack it into Node, you might come across the following error about The ESM Module Loader is Experimental. (dev) root@dev:~$ node –experimental-modules run.ts (node:11333) ExperimentalWarning: The ESM module loader is experimental. (node:11333) Warning: To load an ES module, set “type”: “module” in the package.json or use […]

Programming

Where to Put Your TypeScript Type Annotations

Posted on

I have long held fast to a basic principle of letting type inference do your work for you. That means not adding type annotations unless you have a good reason to. I thought I would supply a bit more information on where to put your TypeScript type annotations and when they add value rather than […]

Programming

Create and Hydrate a TypeScript Class from JSON

Posted on

It is pretty common to want to hydrate a class from a JSON value obtained in a service. If you just parse the JSON you get the properties, but not the behaviours that you expect. As this is a reasonably common problem to solve, it is worthing doing it just once. The following function will […]

Programming

TypeScript NotFunction Type

Posted on

I have been working out how to create a TypeScript NotFunction type for a while, in response to a Stack Overflow question. With the arrival of conditional types, I think there may be a way. It’s not perfect – but it does work. The type works by taking all possible types and converting the type […]

Programming

C# Interfaces: Default Implementations

Posted on

The C# 8.0 announcement included a note on default implementations of interface members. If you’ve been programming for long enough, this might make you a little nervous at first thanks to the hangovers of multiple inheritance. Before you chuck out this idea, though, let’s look at it from a different perspective. I’m going to talk […]

Programming

TypeScript Dictarray Options

Posted on

This is one of those cases where a question keeps cropping up, so lots of people are trying to do something that the TypeScript Compiler doesn’t like very much. Yes, it’s time to talk about the TypeScript Dictarray! What’s a Dictarrary? It’s a dictionary that’s also an array. You know… one of these: const dictarray […]