Skip to content

Instantly share code, notes, and snippets.

View khalidx's full-sized avatar
💭
Engineering for the ☁️

Khalid Zoabi khalidx

💭
Engineering for the ☁️
View GitHub Profile
@khalidx
khalidx / ts-union-to-intersection.md
Last active January 15, 2022 08:38
Convert a TypeScript Union of Types to an Intersection of Types.

TypeScript: Union to Intersection

Convert a TypeScript Union of Types (something | somethingElse) to an Intersection of Types (something & somethingElse).

// See "MergO" from https://github.com/microsoft/TypeScript/issues/26058#issuecomment-553212998
export type UnionToIntersection<U extends object> =
  (U extends object ? (k: U) => void : never) extends (k: infer T) => void
    ? (T extends object ? T : object)
 : object
@khalidx
khalidx / exclude-type-from-array.md
Created March 2, 2022 05:52
Exclude a type from a TypeScript tuple or array.

The following block excludes the type undefined from a TypeScript tuple or array.

You can also replace undefined with something else to exclude another type, but YMMV, depending on the type and how you use it.

export type FilterUndefined<T extends unknown[]> = T extends [] ? [] :
  T extends [infer H, ...infer R] ?
  H extends undefined ? FilterUndefined<R> : [H, ...FilterUndefined<R>] : T
@khalidx
khalidx / log-an-error.md
Created May 12, 2022 16:50
A quick way to log an error object in Node.js.

Obviously, we usually log errors to the console like:

console.error(error)

Problem is, this doesn't always show you all the properties of an error.

If you want to see those properties, use something like:

@khalidx
khalidx / required-form-input.md
Last active April 23, 2023 09:28
A simple way to visually indicate that a form input is required using plain HTML and CSS.

Following this guide will get you the red asterisks* beside form inputs, that you are used to seeing on forms across the internet.

Include the following CSS:

<style>
  label.required:after {
    content: " *";
 color: red;
@khalidx
khalidx / hash-ring.md
Last active August 3, 2022 00:14
An implementation of a "hash ring" for partitioning with consistent hashes.

Usage

const hashRing = createHashRing({
  clients: [
    { address: 'localhost:5001' },
    { address: 'localhost:5002' },
    { address: 'localhost:5003' }
  ]
})
@khalidx
khalidx / restart-mac-audio.md
Created October 7, 2022 14:00
Restart the audio process, for MacBook.

There's a bug on the MacBook Air (M1, 2020) where the audio process crashes or something, and no videos or audio will play.

Here's how to restart the audio process when that happens:

sudo pkill coreaudiod

# or, if the audio process is not responding at all
sudo pkill -9 coreaudiod
@khalidx
khalidx / vscode-debug-typescript.md
Last active December 2, 2023 04:47
A VSCode launch config for debugging a TypeScript project.

This is a simple VSCode launch configuration for debugging TypeScript code that will work with most TypeScript projects.

It allows you to easily debug your project using the awesome debugging experience in VSCode.

It works perfectly for library and CLI projects that are written in TypeScript and are targeting CommonJS.

Usage

  1. Create a .vscode/ folder in your workspace at the top-level, if you don't have that folder already.
@khalidx
khalidx / vscode-devcontainers.md
Created December 2, 2022 09:45
A VSCode DevContainers configuration for dockerized development.

VSCode DevContainers are way better than installing tools locally and coding on your machine; it prevents tool clutter, and ensures your project is portable across your team.

For Node.js / JavaScript / TypeScript development, just create a .devcontainer/devcontainer.json file at the top-level in your workspace with the following contents:

{
  "name": "Node.js & TypeScript",
 "image": "mcr.microsoft.com/devcontainers/typescript-node:18-bullseye"
@khalidx
khalidx / typescript-monorepo.md
Last active December 2, 2023 04:45
A simple setup for a TypeScript monorepo.

There are countless guides online for setting up a TypeScript monorepo.

Most rely on external tools like Lerna, Yarn, Turborepo, Yalc, or something else.

Here's a simple, zero-opinion way to get a TypeScript monorepo going.

First, make a structure like this:

root/
@khalidx
khalidx / ls.ts
Last active November 22, 2023 19:55
List or list and import all files in a directory without any node libraries.
import { join, basename, extname } from 'path'
import { readdir } from 'fs-extra'
/**
* Lists all files in the specified `directory`, without any external libraries.
*
* If `directory` is not specified, `process.cwd()` is used.
*
* Set `includeDirectories` to `true` to also include directory names in the output.
*