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.
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.
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.
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
typeproperty resolve tostring | React.JSXElementConstructor<any>in certain cases instead of the actual type. This should fix that.resolves #2094