diff --git a/src/helpers.ts b/src/helpers.ts new file mode 100644 index 0000000..919cef7 --- /dev/null +++ b/src/helpers.ts @@ -0,0 +1,40 @@ +import { + ElementType, + FunctionComponent, + ComponentClass, + PropsWithChildren, + ReactText +} from "react"; + +export type ReactElementSubset = { + props: PropsWithChildren; + type: ElementType; +}; + +export type ReactElementSubsetWithPrimitive = + | ReactElementSubset + | ReactText + | Array + | null + | undefined; + +export function isFunctionalComponent( + eleType: ElementType +): eleType is FunctionComponent { + return ( + typeof eleType === "function" && // can be various things + !( + (eleType.prototype && eleType.prototype.isReactComponent) // native arrows don't have prototypes // special property + ) + ); +} + +export function isClassComponent( + eleType: ElementType +): eleType is ComponentClass { + return !!( + typeof eleType === "function" && + eleType.prototype && + eleType.prototype.isReactComponent + ); +}