import { MemoExoticComponent, ReactElement, ReactNode, isValidElement } from 'react';

/** Return `true` if given child is give React element */
export function isElement<P>(
  child: Exclude<ReactNode, boolean | null | undefined>,
  element: ((props: P) => JSX.Element | null) | MemoExoticComponent<(props: P) => JSX.Element | null>,
): child is ReactElement<P> {
  if (!isValidElement(child)) {
    return false;
  }

  return child.type === element;
}
