blog icon indicating copy to clipboard operation
blog copied to clipboard

ReactElement/ReactNode/JSX.Element

Open nmsn opened this issue 3 years ago • 0 comments

ReactElement

ReactElement 是含有 props 和 type 属性的对象:

  interface ReactElement<P = any, T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>> {
      type: T;
      props: P;
      key: Key | null;
  }

ReactNode

ReactNode 则是多种类型的集合:

type ReactText = string | number;
type ReactChild = ReactElement | ReactText;

interface ReactNodeArray extends Array<ReactNode> {}
type ReactFragment = {} | ReactNodeArray;

type ReactNode = ReactChild | ReactFragment | ReactPortal | boolean | null | undefined;

JSX.Element

JSX 是一个全局的命名空间,不同的库对 JSX 都可以有自己不同的实现,而 React 的实现方式就是让 JSX.Element 等价于 ReactElement,同时将它的泛型 props 和 type 都设为 any:

declare global {
  namespace JSX {
    interface Element extends React.ReactElement<any, any> { }
  }
}

nmsn avatar Jun 29 '22 13:06 nmsn