typescript posts

Enforcing Localization through Types

When building web applications, enforcing that strings be localized to the userā€™s preferred language can sometimes be achieved via lint rules. But what if we could enforce proper localization using types in Typescript?

Feature Contexts

A common pattern in larger application when trying to track feature usage is to try to namespace events. For example, you might have a button that you want to track clicks on. For the sake of this article, we will use the data-track attribute on elements to track their usage, but you can imagine this works with any tracking implementation where you may need to explicitly call a tracking function (e.g. track(namespace)).

Forbidden Typescript: full/partial objects

When trying to be as type-safe as possible in Typescript, there are times when you may have to dynamically construct an object. You know as a programmer that all the keys will have values, but because you have to dynamically construct the values, the type system doesnā€™t know that the object is complete:

Forbidden Typescript: ensuring all of a type

When creating more complex systems, there are times where you will want to ensure that an array contains every value of a defined type. We have a union of primitive literal types to define the possible values a type can have:

Forbidden Typescript: Get Class Name

We donā€™t get a lot of information using typeof in JavaScript or TypeScript. At most, it tells us whether a value is undefined, number, string, or object. If you want to get the class name using a function, you can use the following extended version of classOf that is originally from ā€œJavaScript: The Definitive Guideā€:

Forbidden Typescript: Enumerating Properties

In ā€œJavaScript: The Definitive Guideā€, there are functions described in 6.5 Enumerating Properties that probably shouldnā€™t be used in production code because they modify their input parameters: extend, merge, restrict, and subtract.

Forbidden Typescript: Using Object.create to clone

In ā€œJavascript: The Definitive Guideā€ there is an example that uses Object.inherit to inherit the prototype change. JavaScript defines a method Object.create that creates a new object using the given argument as the prototype of that object. Translating the examples from The Definitive Guide to Typescript, it looks like:

Debugging slow tsc

I was working on a project and I noticed the tsc check that we ran on pre-commit hooks had become dramatically slower. I was seeing near instant times to type check the entire project go to agonizingly slow minutes to check the project. I was able to narrow the range of commits to some change we made within a month window. Iā€™m sure I could have narrowed it down by continuing to git bisect and re-running time npx tsc, but it was much easier to just check out main and try out the following:

NX NextJS Starter

To kickstart the year, I created a repo that contains a simple starter kit for using NextJS with NX. You can see the repo here: