Last active
August 14, 2025 20:47
-
-
Save moimikey/700e61bd52f3f4ff684e568504a9c6f3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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>; | |
| } |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


Uh oh!
There was an error while loading. Please reload this page.