This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Schema<Out> { | |
parse(value: unknown, path: string): Out; | |
} | |
type Resolve<T> = T extends Schema<infer R> ? Resolve<R> : T; | |
function bool(): Schema<boolean> { | |
return { | |
parse(value, path) { | |
const valid = typeof value === "boolean"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Dialog, Transition} from '@headlessui/react'; | |
import React, {Fragment, ReactNode} from 'react'; | |
import {BiX} from 'react-icons/bi'; | |
interface Props<Y> { | |
isOpen: boolean; | |
close: () => unknown; | |
// Required because used in the callback | |
// eslint-disable-next-line react/no-unused-prop-types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Interceptor { | |
condition(i: number): boolean; | |
execute(i: number): void; | |
} | |
abstract class FizzBuzzUtils { | |
protected isMultipleOf(a: number, multiplier: number) { | |
return a % multiplier === 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Constructor<T> = new (...args: any[]) => T; | |
type Then<T, R> = (value: T) => R; | |
export function ifInstance<T, R>(instance: Constructor<T>, then: Then<T, R>) { | |
return (value: unknown) => { | |
if (value instanceof instance) { | |
return then(value); | |
} | |
throw value; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Multiplication in raw TypeScript. Please, please do not ever ever ever use this | |
type TupleOfLength< | |
T, | |
L extends number, | |
R extends T[] = [] | |
> = R["length"] extends L ? R : TupleOfLength<T, L, [...R, T]>; | |
type FlattenResult< | |
T extends 0[][], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type RemoveOn<T extends string> = T extends `on${infer R}` ? R : T; | |
type PropsFor<El extends keyof JSX.IntrinsicElements> = JSX.IntrinsicElements[El]; | |
type OnProps<El extends keyof JSX.IntrinsicElements> = Uncapitalize<RemoveOn<Extract<keyof PropsFor<El>, `on${string}`>>>; | |
type HandlerFor<El extends keyof JSX.IntrinsicElements, Ev extends OnProps<El>> = NonNullable<PropsFor<El>[`on${Capitalize<Ev>}` & keyof PropsFor<El>]>; | |
// Creating with a callback | |
function handlerFor<El extends keyof JSX.IntrinsicElements, Ev extends OnProps<El>>(event: [El, Ev], handler: HandlerFor<El, Ev>) { | |
return handler; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function mapOrder<T, Key extends keyof T>(array: T[], order: Array<T[Key]>, key: Key) { | |
return array.sort((a, b) => { | |
const A = a[key]; | |
const B = b[key]; | |
if (order.indexOf(A) > order.indexOf(B)) { | |
return 1; | |
} | |
return -1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checks if a value exists in an array in a type safe manner | |
* @param value Value to check | |
* @param arr An array of possible matches | |
* @returns A boolean indicating if a passed value is one of the items in the array | |
*/ | |
export function is<V extends string | number, Arr extends readonly [V, ...V[]]>( | |
value: unknown, | |
arr: Arr, | |
): value is Arr[number] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { setInterval } from "timers/promises"; | |
const createCounter = (initial = 0) => { | |
let value = initial; | |
return () => ++value; | |
}; | |
const start = Date.now(); | |
for await (const incr of setInterval(1000, createCounter())) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import y from "yaml"; | |
export class TreeNode<T> { | |
public readonly left?: TreeNode<T>; | |
public readonly right?: TreeNode<T>; | |
public readonly value: T; | |
constructor( | |
value: T, | |
left?: TreeNode<T>, |
NewerOlder