This is a super quick one. Be super-careful about how you name your custom type guards to stop consumers falling into a trap. Basically, the name you give a custom type guard and the return type you specify form a kind of promise that you have to be able to keep. Take this example: functionContinue reading “Naming TypeScript Custom Type Guards”
Tag Archives: typescript
JQuery and TypeScript – We Have a Big Problem With jquery.d.ts
This is a call to all my TypeScript connections. This is a call to all. We have a big problem with the official Definitely Typed definition for jQuery. The most fundamental definition for the JQuery interface is incorrect, as you can see in the snippet from jquery.d.ts below. We can fix it, but not withoutContinue reading “JQuery and TypeScript – We Have a Big Problem With jquery.d.ts”
Avoid Expensive innerHTML Manipulation with insertAdjacentHTML
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 avoidContinue reading “Avoid Expensive innerHTML Manipulation with insertAdjacentHTML”
Expecting One Out Of Two Members in TypeScript Types
We’re going to use a simple restaurant set-menu example to explore TypeScript types. By the end of this thought-process, we’re going to be able to require one out of two members that are present on a type. Here’s the scenario. You have a set three-course menu. It looks like this: type SetMenu = { starter:Continue reading “Expecting One Out Of Two Members in TypeScript Types”
The ESM Module Loader is Experimental
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) [email protected]:~$ 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 useContinue reading “The ESM Module Loader is Experimental”
Where to Put Your TypeScript Type Annotations
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 thanContinue reading “Where to Put Your TypeScript Type Annotations”
Genius in Hindsight: TypeScript Type Annotations
When TypeScript first landed in public view in October 2012, the type annotations looked a bit funky. If you were a student of type theory, they would have been familiar; but most programmers wouldn’t have seen a type annotation like this before: var name: string; Given the popularity of putting type names before variable names,Continue reading “Genius in Hindsight: TypeScript Type Annotations”
Visual Studio Code IntelliCode Extension Preview
IntelliCode brings AI-assisted power-ups to your auto-completion. It has been in preview within Visual Studio for some time (you can read about Visual Studio IntelliCode here) – but it has now landed in Visual Studio Code, which is exceptionally handy if you’re a TypeScript programmer like me. So what is IntelliCode? It’s a simple VSCodeContinue reading “Visual Studio Code IntelliCode Extension Preview”
Create and Hydrate a TypeScript Class from JSON
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 willContinue reading “Create and Hydrate a TypeScript Class from JSON”
TypeScript NotFunction Type
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 typeContinue reading “TypeScript NotFunction Type”