2ality – JavaScript and more
Today’s announcement by Microsoft: [...] we’ve begun work on a native port of the TypeScript compiler and tools. The native implementation will drastically improve editor startup, reduce most build times by 10×, and substantially reduce memory usage. This blog post looks at some of the details behind the news.
In this blog post, we explore what unions and intersections of object types can be used for in TypeScript.
Roughly, TypeScript is JavaScript plus type information. The latter is removed before TypeScript code is executed by JavaScript engines. Therefore, writing and deploying TypeScript is more work. Is that added work worth it? In this blog post, I’m going to argue that yes, it is. Read it if you are skeptical about TypeScript but interested in giving it a chance.
Read this blog post if you are a JavaScript programmer and want to get a rough idea of what using TypeScript is like (think first step before learning more details): How TypeScript code is different from JavaScript code. How to run TypeScript code. How to edit TypeScript code in an IDE. Etc. Note: This blog post does not explain why TypeScript is useful. If you want to know more about that, you can read my TypeScript sales pitch.
Now that Node.js has built-in support for TypeScript, we can use it as the foundation of a simple playground that lets us interactively explore TypeScript code.
In this blog post, we explore how we can test that complicated TypeScript types work as expected. To do that, we need assertions at the type level and other tools.
The TypeScript handbook makes an interesting statement: “Often, the checks in a conditional type will provide us with some new information. Just like narrowing with type guards can give us a more specific type, the true branch of a conditional type will further constrain generics by the type we check against.” In this blog post, we’ll see that this goes further than you may think.
I have published the repository nodejs-type-stripping which demonstrates how to implement a package with a bin script that is written directly in TypeScript (no transpilation).
In this blog post, we look at the special TypeScript type never which, roughly, is the type of things that never happen. As we’ll see, it has several surprising number of applications.
In this blog post, we explore two equivalent notations for Arrays in TypeScript: T[] and Array<T>. I prefer the latter and will explain why.
In this blog post, we examine how TypeScript handles JavaScript symbols at the type level. If you want to refresh your knowledge of JavaScript symbols, you can check out chapter “Symbols” of “Exploring JavaScript”.
In TypeScript, conditional types let us make decisions (think if-then-else expressions) – which is especially useful in generic types. They are also an essential tool for working with union types because they let use “loop” over them. Read on if you want to know how all of that works.
A mapped type is a loop over keys that produces an object or tuple type and looks as follows: {[PropKey in PropKeyUnion]: PropValue} In this blog post, we examine how mapped types work and see examples of using them. Their most importing use cases are transforming objects and mapping tuples.
In this blog post, we explore how we can extract parts of composite types via the infer operator. It helps if you are loosely familiar with conditional types. You can check out section “Conditional types” in “Tackling TypeScript” to read up on them.
TypeDoc now lets us refer to parts of other files via {@includeCode}. In this blog post, I explain how that works and why it’s useful.
TypeScript’s satisfies operator lets us check the type of a value (mostly) without influencing it. In this blog post, we examine how exactly it works and where it’s useful.
In this blog post, we look at how can make things “read-only” in TypeScript – mainly via the keyword readonly.
During the last two years, ESM support in TypeScript, Node.js and browsers has made a lot of progress. In this blog post, I explain my modern setup that is relatively simple – compared to what we had to do in the past:
JavaScript’s Arrays are so flexible that TypeScript provides two different kinds of types for handling them: Array types for arbitrary-length sequences of values that all have the same type – e.g.: Array<string> Tuple types for fixed-length sequences of values where each one may have a different type – e.g.: [number, string, boolean] In this blog post, we look at the latter – especially how to compute with tuples at the type level.
In this blog post, we take a closer look at template literal types in TypeScript: While their syntax is similar to JavaScript’s template literals, they operate at the type level. Their use cases include: Static syntax checking for string literals Transforming the casing of property names (e.g. from hyphen case to camel case) Concisely specifying large string literal union types
The ECMAScript proposal “RegExp escaping” (by Jordan Harband and Kevin Gibbons) specifies a function RegExp.escape() that, given a string text, creates an escaped version that matches text – if interpreted as a regular expression. This proposal is currently at stage 3.
In this blog post, we take a closer look at TypeScript enums: How do they work? What are their use cases? What are the alternatives if we don’t want to use them? The blog post concludes with recommendations for what to use when.
I never felt confident about my tsconfig.json. To change that, I went through the official documentation, collected all common options, and documented them in this blog post: This knowledge will enable you to write a tsconfig.json that is cleaner and that you’ll fully understand. If you don’t have the time to read the post, you can jump to the summary at the end where I show the tsconfig.json that I use now – along with recommendations for adapting it to different use cases (npm package, app, etc.). I also link to the tsconfig.json recommendations by several well-known TypeScript programmers. (I went through them when I researched this post.) I’m curious what your experiences with tsconfig.json are: Do you agree with my choices?
Traditionally, we could only apply regular expression flags such as i (for ignoring case) to all of a regular expression. The ECMAScript feature “Regular Expression Pattern Modifiers” (by Ron Buckton) enables us to apply them to only part of a regular expression. In this blog post we examine how they work and what their use cases are. Regular expression pattern modifiers attributes reached stage 4 in October 2024 and will probably be part of ECMAScript 2025.
The ECMAScript feature “Import Attributes” (by Sven Sauleau, Daniel Ehrenberg, Myles Borins, Dan Clark and Nicolò Ribaudo) helps with importing artifacts other than JavaScript modules. In this blog post, we examine what that looks like and why it’s useful. Import attributes reached stage 4 in October 2024 and will probably be part of ECMAScript 2025.
Starting with v23.6.0, Node.js supports TypeScript without any flags. This blog post explains how it works and what to look out for.
In this blog post, we look at how WebAssembly has become an ecosystem for many programming languages and what technologies enable that.
You can subscribe to this RSS to get more information