Skip to content

Instantly share code, notes, and snippets.

@moimikey
Last active August 14, 2025 20:47
Show Gist options
  • Save moimikey/700e61bd52f3f4ff684e568504a9c6f3 to your computer and use it in GitHub Desktop.
Save moimikey/700e61bd52f3f4ff684e568504a9c6f3 to your computer and use it in GitHub Desktop.
export type Curry<F extends (...args: readonly unknown[]) => unknown> =
Parameters<F> extends readonly [infer First, ...infer Rest]
? (arg: First) => Rest extends readonly []
? ReturnType<F>
: Curry<(...args: Rest) => ReturnType<F>>
: ReturnType<F>;
export function curry<F extends (...args: readonly unknown[]) => unknown>(fn: F): Curry<F> {
function curried(...providedArgs: readonly unknown[]): unknown {
if (providedArgs.length >= fn.length) {
return fn(...(providedArgs.slice(0, fn.length) as Parameters<F>));
}
return (...newArgs: readonly unknown[]) => {
return curried(...providedArgs, ...newArgs);
};
}
return curried as Curry<F>;
}
@moimikey
Copy link
Author

enum SomeType {
  A = 'A',
  B = 'B',
}

type TypeMap = {
  [SomeType.A]: string;
  [SomeType.B]: number;
}

type ReactiveFunction<T> = (...args: unknown[]) => T;

function useReactive<T extends keyof TypeMap>(
  fn: (...args: unknown[]) => unknown,
  type: T
): ReactiveFunction<TypeMap[T]>;

function useReactive<T extends keyof TypeMap>(
  fn: (...args: unknown[]) => unknown,
  type: T
): ReactiveFunction<TypeMap[T]> {
  
  return (...args: unknown[]): TypeMap[T] => {
    const result = fn(...args);
    
    switch (type) {
      case SomeType.A:
        return String(result) as TypeMap[T];
      case SomeType.B:
        return Number(result) as TypeMap[T];
      default:
        return result as TypeMap[T];
    }
  };
}

// Usage
const a = useReactive(() => 42, SomeType.A); // Type: (...args: unknown[]) => string
const b = useReactive(() => "hello", SomeType.B); // Type: (...args: unknown[]) => number

// When you call them:
const stringResult = a(); // Type: string
const numberResult = b(); // Type: number

@moimikey
Copy link
Author

image

@moimikey
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment