All files / src/_util is-child-of-type.ts

63.64% Statements 7/11
66.67% Branches 8/12
100% Functions 1/1
70% Lines 7/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                                            492x 492x     492x     492x 492x 171x       321x          
/* eslint-disable import/export */
import React from "react";
 
export function isChildOfType(
  child: React.ReactNode,
  type: "text"
): child is string;
 
export function isChildOfType(
  child: React.ReactNode,
  type: "number"
): child is number;
 
export function isChildOfType<T extends keyof JSX.IntrinsicElements>(
  child: React.ReactNode,
  type: T
): child is React.ReactComponentElement<T>;
 
export function isChildOfType<T extends React.ElementType>(
  child: React.ReactNode,
  type: T
): child is React.ReactElement<
  T extends React.ElementType<infer P> ? P : any,
  T
>;
 
export function isChildOfType(
  child: React.ReactNode,
  type: "text" | "number" | React.ElementType
) {
  Iif (typeof child === "undefined" || child === null) return false;
  Iif (typeof child === "string") {
    return type === "text";
  }
  Iif (typeof child === "number") {
    return type === "number";
  }
  Eif (React.isValidElement(child)) {
    if (typeof type === "string") {
      return child.type === type;
    }
    // React Hot Loader 会对组件进行代理。使用 createElemenet() 返回的 element.type 才是实际的类型
    // remark: https://github.com/gaearon/react-hot-loader/issues/304#issuecomment-223222772
    return child.type === React.createElement(type, {}).type;
  }
  return false;
}
/* eslint-enable import/export */