Skip to content

Instantly share code, notes, and snippets.

View gvergnaud's full-sized avatar
🚀

Gabriel Vergnaud gvergnaud

🚀
View GitHub Profile
@gvergnaud
gvergnaud / 0_types.ts
Created February 27, 2024 23:30
prose-mirror-json-schema
type TextMarks =
| { type: "bold" }
| { type: "italic" }
| { type: "code" }
| { type: "strike" }
| { type: "textStyle"; attrs: Partial<CSSStyleDeclaration> }
| {
type: "link";
attrs: {
href: string;
import { createLiveState, crdtSchema } from "@/lib/live-state";
export type NotebookJSON = {
title: string;
description: string;
cells: TextCell[];
};
const notebookCRDTSchema = {
title: crdtSchema.LiveText(),
@gvergnaud
gvergnaud / 1-raf-throttle.ts
Last active August 9, 2023 19:36
raf-throttle.ts
type RafState<Args> =
| { type: 'waiting'; latestArgs: Args; rafId: number }
| { type: 'idle' };
export const animationFrameThrottle = <Args extends any[]>(
callback: (...args: Args) => unknown,
): ((...args: Args) => void) & { cancel: () => void } => {
let state: RafState<Args> = { type: 'idle' };
const cancel = () => {
export function usePrevious<T>(value: T): T | undefined {
const ref = React.useRef<T>();
React.useEffect(() => {
ref.current = value;
});
return ref.current;
}
useEffect(() => {

})

<>
  <Child log1 />
  {console.log("log3")}
  <Child log2 />
&gt;
import { arg0, Call, Constant, Eval, Fn, Objects, Tuples } from "../src/index";
type Ok<A> = { tag: "Ok"; value: A };
type Err<B> = { tag: "Err"; value: B };
type Result<A, B> = Ok<A> | Err<B>;
type ParseError<expected = unknown, encountered = unknown> = {
expected: expected;
type MergeParameters3<T extends readonly UnknownFunction[]> = H.Pipe<
T,
[
H.Let<"parametersList", H.Tuples.Map<H.Functions.Parameters>>,
H.Let<
"longestLength",
H.ComposeLeft<
[
H.Get<"parametersList">,
H.Tuples.Map<H.Tuples.Length>,
// a conditional type is just type-level code branching
// A extends B means "A is assignable to B"
type Test1 = "hello" extends string ? true : false;
// ^?
type Test2 = 2 extends string ? true : false;
// ^?
@gvergnaud
gvergnaud / mapped-types-as-recursive-distributive-union-type.ts
Created January 8, 2023 18:27
Can you express any mapped types in terms of recursive + distributive union type?
// mapped type version
type Fn<T> = {
[K in keyof T]: [K, T[K]] /* arbitrary fn using K and T[k] */;
};
// distributive union version
type Fn2<T> = FromEntries<
keyof T extends infer K
? K extends keyof T
? [K, [K, T[K]] /* arbitrary fn using K and T[k] */]
import type { Equal, Expect } from '@type-challenges/utils'
type Assign<A, B> = {
[K in keyof A | keyof B]: K extends keyof B
? B[K]
: K extends keyof A
? A[K]
: never;
}