Skip to content

Instantly share code, notes, and snippets.

@jtmthf
Created May 5, 2018 21:03
Show Gist options
  • Save jtmthf/0af7a8fcd5ea889a2215d5acefb4d5c5 to your computer and use it in GitHub Desktop.
Save jtmthf/0af7a8fcd5ea889a2215d5acefb4d5c5 to your computer and use it in GitHub Desktop.
import * as React from 'react';
import { Component, ComponentClass, SFC } from 'react';
type Constructor<T = {}> = new (...args: any[]) => T;
type TagProps<Tag extends keyof JSX.IntrinsicElements> = {
is: Tag;
innerRef?: JSX.IntrinsicElements[Tag]['ref'];
} & JSX.IntrinsicElements[Tag];
type StatelessComponentProps<P> = {
is: SFC<P>;
} & P;
type ComponentClassProps<T, P> = {
is: ComponentClass<P>;
innerRef?: (instance: T | null) => any;
} & P;
type Props<T> = T extends 'div'
? JSX.IntrinsicElements['div']
: T extends keyof JSX.IntrinsicElements
? TagProps<T>
: T extends Component<infer P>
? ComponentClassProps<T, P>
: T extends SFC<infer P> ? StatelessComponentProps<P> : never;
class Any<T = 'div'> extends Component<Props<T>> {
static ofType<T>() {
return Any as Constructor<Any<T>>;
}
render() {
const { is: Cmp = 'div', innerRef, ...props } = this.props as any;
return <Cmp ref={innerRef} {...props} />;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment