deephaven / web-client-ui

Deephaven Web Client UI
Apache License 2.0
28 stars 30 forks source link

isElementOfType - Not passing derived `T` type to `ReactElement` #2094

Closed bmingles closed 1 month ago

bmingles commented 1 month ago

The isElementOfType util is not currently providing the 2nd generic arg to ReactElement. This results in certain cases failing to narrow types which is the whole point of the type guard.

export function isElementOfType<T>(
  node: ReactNode,
  type: T
): node is ReactElement<InferComponentProps<T>> {
  return isValidElement(node) && node.type === type;
}

We should be able to fix this by constraining T a bit more and then passing it as the 2nd arg.

export function isElementOfType<
  T extends string | JSXElementConstructor<any> =
    | string
    | JSXElementConstructor<any>,
>(node: ReactNode, type: T): node is ReactElement<InferComponentProps<T>, T> {
  return isValidElement(node) && node.type === type;
}