Open mreid21 opened 1 month ago
If you can solve it and make a PR, we’d love the help.
I think I can do it.
I came up with a fix, but it breaks propTypes
. Since the TreeView uses forwardRef
, I have to use a type assertion, write a custom forwardRef
function, or pass a ref in the component props. The first two approaches break the TreeView.propTypes
and the third would be a breaking change.
I think the approach we’d most likely be willing to accept is a custom type assertion since it’s my belief that a custom type assertion would provide the same runtime capabilities of prop types. And it’s better than a breaking change.
Can you submit a PR and tag @mellis481 for review?
btw, if it’s possible to avoid bringing in a library like Zod for type validation, that’s significantly preferable.
The main problem is that when I use a type assertion to capture the generic type I get
Property 'propTypes' does not exist on type '<T extends IFlatMetadata>(props: ITreeViewProps<T> & { ref?: ForwardedRef<HTMLUListElement> | undefined; }) => Element'
I don't know much about propTypes, maybe I'm missing something in the type assertion?
const TreeView = React.forwardRef(TreeViewInner) as <T extends IFlatMetadata>(
props: ITreeViewProps<T> & { ref?: React.ForwardedRef<HTMLUListElement> },
) => ReturnType<typeof TreeViewInner>;
If I cast forwardRef
and do
TreeViewInner.propTypes
instead of TreeView.propTypes
then I still get type inference without the error, but I'm not sure if the propTypes
are still captured since I'm doing it on the non forwardRefed version.
I’m sorry that I can’t help. I did the best I was capable of when I wrote the original PR the code that you’re looking at.
I created a PR. I'm not sure how propTypes
will be affected, but it implements the desired behavior. Once React allows components to accept ref
directly, forwardRef
can be ditched entirely and this code can become way simpler.
Describe the bug The type of
metadata
innodeRenderer
is being inferred incorrectly.Code That Causes The Issue
To Reproduce I created a minimal reproduction but you can do the following.
metadata
fromelement
in the node renderer prop on the tree.https://codesandbox.io/p/devbox/8rc7df?file=%2Fsrc%2FBasicTreeView.tsx%3A40%2C5-49%2C7
Expected behavior The generic type of
metadata
should be inferred in the same manner as it is inflattenTree
as shown in my reproduction.Note: I am almost certain it is caused by this.
There is no generic being passed to
INodeRenderer
so it uses the default specified inINode
. You can even see thatmetadata
uses the default type in the repro. It would also explain why there is no type error.I also reproduced the type issue here: https://www.typescriptlang.org/play/?ssl=37&ssc=1&pln=1&pc=1#code/C4TwDgpgBAkgYgGwIbALIWEgJipUC8UAShAMYD2ATlgDwBQUUAzsJQJYB2A5gDQPOtOXKAB8oHAK4BbAEYRKoqDPLkEEJB0USOWCADNOELIskIEdAHwBuOnU7B5epKWgwAcuV01UUCAA8HHSZYRBR0TBxMAhDkNAxsXAsoAG9+RgB6ACpMqABBcXIOAFoOCC4UNgA3aHsy+ShgAAsUKG02AEcJCAQQKDZdDmA2AwhgpugOT2hM9LS+rAAuAXZuE2k5ShtGDOyoAFUmIwbyKCkUUkaoQqgAawhesEpR4Jm5jiQpCCWWFa4t7ayOVymiQlEoSF6AHc2E0Go0alhguQ9HDoBc2AgsE9NJNdC9ZtsoOjMdilgAKH5CNayeQASgA2gBdf47HIAFXhUDAoIggyuKPGBV0VnEEjMUD0VFRUEoKmAQumBO23OxwHJlNWYkkNMotLWZhZUEB+0OxmAJ04WDYpBQR0h8PGCjwuJqwRk4I4F2OSmgSBkaigCHI2CgSCYIE9UEieEKAFE-NydK9CWwmAAhD0XAD8S2UqnUHENxoO8iKugMpWMnwiuCgye21YSmBzUFQ-wAvrZGLVKE4XLAPLoSDp5PIAAqysDBVKE40c6DkGQAKzI8qaLSej1GvOAY052N0T2MLrrSsY3Qgn0GS3cUw7tjoFA4LAaozVA6mw8P48nwUIyQvK931Sd5PiWAByBwWHAnhG2jJZklFKQlgARgABigds+H6JYACIUNwvhiSxXkliZPgVR3JZTHMdtOzodJ0ncAA1XIABkYAAESgNNYwACVyZiYAAeSIOhQEgKA2RQ6IJIgZFXxYelwMAndwMZZS4NwdTfACXlERSbVqPWeR2ygLMGkoLooGoiBqkoB9GCAA