-
Notifications
You must be signed in to change notification settings - Fork 31
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2099 +/- ##
=======================================
Coverage 46.59% 46.59%
=======================================
Files 679 679
Lines 38598 38598
Branches 9779 9779
=======================================
Hits 17986 17986
Misses 20560 20560
Partials 52 52
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
isElementOfType
Improved type inference
): node is ReactElement<InferComponentProps<T>> { | ||
export function isElementOfType< | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
T extends string | JSXElementConstructor<any> = |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
): node is ReactElement<InferComponentProps<T>> { | ||
export function isElementOfType< | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
T extends string | JSXElementConstructor<any> = |
There was a problem hiding this comment.
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.
I missed the 2nd generic argument when I first wrote this util which made the
type
property resolve tostring | React.JSXElementConstructor<any>
in certain cases instead of the actual type. This should fix that.resolves #2094