Skip to content

Instantly share code, notes, and snippets.

View devanshj's full-sized avatar
🌸

Devansh Jethmalani devanshj

🌸
View GitHub Profile
@devanshj
devanshj / exact-type-alias.md
Last active June 5, 2020 00:17
TypeScript: Exact type for unions

Declarative writing style and "space"

(Disclaimer: this is not to encourage writing expressions all the time no matter what, I'm just pointing out some things that makes me want to write expressions more.)

What do you think is the difference between...

function parity(x) {
    if (x % 2 === 0) {
        return Parity.EVEN
@devanshj
devanshj / index.ts
Last active August 24, 2020 20:21
steve-ruiz-recursive-state-type
// https://twitter.com/steveruizok/status/1297977525755424771
declare const machine:
<D extends Definition<D>>(definition: D) =>
({ withTransitions: <T extends { [event in string]: State<D> }>(transition: T) => D & T })
declare const childMachine:
<D extends Definition<D>>(config: D) => D & Tag<"ChildMachine">;
type Definition<D> =
const useStates = <S extends string>({ initial, effects }: {
initial: S,
effects:
& { [K in S]: () => PromiseOrValue<NullOrValue<Exclude<S, K>>> }
& { ERROR: (error: unknown) => PromiseOrValue<NullOrValue<S>> }
}): StateTuple<WithError<S>> => {
let [state, setState] = useState(initial as WithError<S>);
const runEffect = (state: WithError<S>, error?: unknown) => {
let returned = state === "ERROR" ? effects[state](error) : effects[state]();
if (returned instanceof Promise) {
@devanshj
devanshj / Sp.ts
Last active March 2, 2022 12:22
react spring wrapper
// ps: this is might be a little outdated compared to one in my closed source project
import { useSpring, to, SpringValue, animated } from "@react-spring/web"
import { assertNever, Ex } from "./";
import * as Re from "react";
namespace Sp {
export type Springable = number | string | readonly (string | number)[];
export interface Spring<T extends Springable> { __Spring: void, __T: T }
export interface UnknownSpring extends Spring<Springable> {}