Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: isElementOfType Improved type inference #2099

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/react-hooks/src/ElementUtils.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { createElement } from 'react';
import { Text } from '@adobe/react-spectrum';
import { ItemElement, Item, Text } from '@deephaven/components';
import { isElementOfType } from './ElementUtils';

beforeEach(() => {
jest.clearAllMocks();
expect.hasAssertions();
});

describe('isElementOfType', () => {
function MockComponent() {
return null;
Expand All @@ -21,4 +26,16 @@ describe('isElementOfType', () => {
expect(isElementOfType(element, type)).toBe(expected);
}
);

it('should derive the `type` prop', () => {
const element: ItemElement = createElement(Item);

if (isElementOfType(element, Item)) {
// This is a type check that verifies the type guard narrows this to the
// `Item` function instead of `string | JSXElementConstructor<any>`. This
// proves that #2094 is working as expected. Namely, the compiler will
// complain if it thinks `type` could be a string.
expect(element.type.name).toEqual(Item.name);
}
});
});
18 changes: 13 additions & 5 deletions packages/react-hooks/src/ElementUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { isValidElement, ReactElement, ReactNode } from 'react';
import {
isValidElement,
JSXElementConstructor,
ReactElement,
ReactNode,
} from 'react';
import { InferComponentProps } from '@deephaven/utils';

/**
Expand All @@ -7,10 +12,13 @@ import { InferComponentProps } from '@deephaven/utils';
* @param type The type to check against
* @returns True if the node is a React element of the specified type
*/
export function isElementOfType<T>(
node: ReactNode,
type: T
): node is ReactElement<InferComponentProps<T>> {
export function isElementOfType<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
T extends string | JSXElementConstructor<any> =
| string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious for learning purposes, what is the reasoning for having to separately extend string here, not any other types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Element types are either string or a factory function. e.g. 'div' | 'span' | SomeComponent. This also matches the signature of the ReactElement 2nd generic arg

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have string | JSXElementConstructor<any> defined as a type above to make this a little nicer.

// eslint-disable-next-line @typescript-eslint/no-explicit-any
| JSXElementConstructor<any>,
>(node: ReactNode, type: T): node is ReactElement<InferComponentProps<T>, T> {
return isValidElement(node) && node.type === type;
}

Expand Down
Loading