Conditionally making optional properties mandatory in typescript

A common case in applications is to work with types where certain properties are nullable. However, it is also common to need derived types of these same types which are stricter around nullablity of properties.

Let’s imagine a note taking app: We have a `Notebook` type where `id` is nullable because unsaved notebooks haven’t been assigned an `id` yet. However after saving it, we know for a fact that a `Notebook` instance will have an `id` and ideally we shouldn’t have to say `!` in the rest of the application.

So, in this post we will explore a generic way to derive types like `SavedNotebook` where certain properties are conditionally mapped to non-optional.

Continue Reading →

Dealing with circular type references in Mobx-state-tree

A frequently occuring issue when creating interrelated MST models, is that of circular type references.

Usually we don’t don’t have to explicitly define interfaces for our models, because they can be inferred for us through the APIs exposed by MST. However, when defining models that depend on each other, this falls short because TypeScript’s type-inference is not good enough to circular dependencies.

This post outlines how we can augment the inferred types in MST to resolve such cases.

Continue Reading →