Skip to content

Instantly share code, notes, and snippets.

@pfgray
Created March 11, 2019 01:18
Show Gist options
  • Save pfgray/d183df1006c24938dde16f3b8a3bf1ce to your computer and use it in GitHub Desktop.
Save pfgray/d183df1006c24938dde16f3b8a3bf1ce to your computer and use it in GitHub Desktop.
import * as React from "react";
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type Func = (...args: any) => any;
export type Lit = string | number | boolean | undefined | null | void | {};
export const tuple = <T extends Lit[]>(...args: T) => args;
type RenderProp<Props, SuppliedValue> = React.FC<
Props & { children: (s: SuppliedValue) => React.ReactNode }
>;
interface InferableHOC<ProvidedProps> {
<B extends ProvidedProps>(c: React.ComponentType<B>): React.ComponentType<
Omit<B, keyof ProvidedProps>
>;
}
type ArgumentTypes<F extends Function> = F extends (...args: infer A) => any
? A
: never;
type HocGenerator<Arguments> = <Z>(
mapper: (a: Arguments) => Z
) => InferableHOC<Z>;
const makeHookCompat = <Hook extends Func, Props extends object>(
hook: Hook,
mapper: (props: Props) => ArgumentTypes<Hook>
): [RenderProp<Props, ReturnType<Hook>>, HocGenerator<ReturnType<Hook>>] => {
return null as any;
};
const myHook: (s: string) => number = s => s.length;
type MyProps = { foo: number };
// works fine!
const [RP, makeHoc] = makeHookCompat(myHook, (props: MyProps) => tuple("foo"));
const myHoc = makeHoc(num => ({
foo: num
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment