Skip to content

Instantly share code, notes, and snippets.

@sidola
sidola / assume-defined.ts
Created July 5, 2022 07:45
Ensures a given property is defined and changes its type accordingly.
/**
* Creates a new type where prop T on object K is required and
* non-nullable.
*/
type DefinedProp<T, K extends keyof T> = (
Omit<T, K> &
{ [key in K]-?: Exclude<T[key], undefined> }
)
/**

React Rendering Cheat Sheet

Last updated: 2023-10-13 (React v18)

Personal cheat sheet for react rendering details. Most of the stuff here is sourced from Mark Erikson's great blogpost. Don't trust anything you read here, go read his post instead.

Code examples can be found here: website, repo


/**
* Calls the given list of promises in sequence, ensuring a minimum
* delay of `staggerMs` between calls.
*
* @param funcs Array of promises to call.
* @param staggerMs The minimum time between promise calls. If a
* promise runs longer than this, the next one is fired as soon as the
* previous is done.
*/
export async function staggerCalls(funcs: (() => Promise<any>)[], staggerMs: number) {
@sidola
sidola / gitconfig.properties
Last active May 18, 2022 11:49
Misc git aliases
[alias]
# Undo the latest commit without deleting the files
pop-head = reset HEAD~1
# Force push with a lease
# https://git-scm.com/docs/git-push#Documentation/git-push.txt---no-force-with-lease
push-lease = push --force-with-lease
# Rename the previous commit
rename = commit --amend
# Lists all branches
branches = branch -l
@sidola
sidola / typescriptreact.json
Created January 5, 2022 09:37
TSX Snippets VSCode
{
// https://snippet-generator.app/
"React component": {
"prefix": "rcomp",
"body": [
"type ${1:Component}Props = {",
"",
"}",
"",
"export const ${1:Component} = (props: ${1:Component}Props): JSX.Element | null => {",
@sidola
sidola / pub-sub-class.ts
Last active May 14, 2022 11:23
Experimental PubSub impl. as a class
class PubSub<T> {
private handlers: { [key: string]: any[] } = {}
public subscribe<E extends keyof T & string>(
event: E,
callback: (message: T[E]) => void
) {
const list = this.handlers[event] ?? []
list.push(callback)
@sidola
sidola / functions-optional.d.ts
Last active January 4, 2022 09:14
Make function properties optional
/*
Why are we using Exclude<..., undefined>? Because when extracting
keys, optional properties will resolve to undefined. See here:
https://github.com/microsoft/TypeScript/issues/34992#issuecomment-551822423
---
The Optional/Required types below are leveraging the fact that:
@sidola
sidola / chainSort.ts
Last active June 22, 2021 16:03
Chain sorting, with Typescript support
/**
* A type that only allows primitive property keys
*/
type PrimitiveKeysOnly<T> = {
[P in keyof T]: T[P] extends number | string | boolean ? P : never
}[keyof T]
type SortableProp<T> = {
prop: PrimitiveKeysOnly<T>
order: 'desc' | 'asc'
@sidola
sidola / sqliteBool.ts
Last active June 17, 2021 18:08
Utility method for dealing with SQLites boolean type in a Typescript codebase
type SqliteBool<T extends number | boolean> = T extends number ? boolean : number
/**
* Converts an sqlite bool (0 or 1) to a real bool, and vice versa.
*
* @example
* sqliteBool(1) // => true
* sqliteBool(true) // => 1
*/
export function sqliteBool<T extends number | boolean>(value: T): SqliteBool<T> {
@sidola
sidola / pubsub.ts
Last active January 24, 2023 05:13
Basic typesafe pub-sub implementation in Typescript
/* ------------------------------------------
Alternative impl. as a class can be found here: https://gist.github.com/sidola/eaf987d8c4c7e8445b61dc07c33a842f
Has a way smaller footprint and less typescript magic to grasp.
------------------------------------------ */
/**
* Defines the function type of the publish function.
*