1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/packages/utility-features/utilities/src/isReactNode.ts
Sebastian Malton 2ca77ecfe8 fix: Fix StrictReactNode not accepting multiple children
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-05-15 11:56:30 -04:00

36 lines
883 B
TypeScript
Executable File

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import { isObject } from "./type-narrowing";
// ref: https://changelog.com/posts/the-react-reactnode-type-is-a-black-hole
export type StrictReactFragment =
| {
key?: string | number | null;
ref?: null;
props?: {
children?: StrictReactNode;
};
};
export type StrictReactNode =
| React.ReactElement
| React.ReactText
| StrictReactFragment
| React.ReactPortal
| Iterable<StrictReactNode>
| boolean
| null
| undefined;
export function isReactNode(node: unknown): node is StrictReactNode {
return (isObject(node) && React.isValidElement(node))
|| Array.isArray(node) && node.every(isReactNode)
|| node == null
|| typeof node !== "object";
}