Skip to content

isElementOfType - Not passing derived T type to ReactElement #2094

@bmingles

Description

@bmingles

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;
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions