TypeScript Snippets

Ever-growing list of helpful TypeScript snippets I'm collecting over the years.
Want to share yours? Send me DM on Twitter!

🚧 Snippets are still under development 🚧


Type guard used for removing nullability from generic type. Great for filtering.

function isNotNullOrUndefined<T>(x: T | null | undefined): x is T {
  return x !== null && x !== undefined;

Pattern match object

Using indexed access to object with keys equal to string literals (will work with number literals as well) allows to easy pattern match union type.

const theme = "dark";
const color = {
  dark: "#fff",
  light: "#000",