-
Notifications
You must be signed in to change notification settings - Fork 47.4k
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
Bug: JSX loses component type definition #26934
Comments
Hello, @yairopro I think this is the behavior of jsx when using JSX, the element's type is inferred based on the component's type declaration and the props passed to it. However, once the JSX code is transpiled to JavaScript, the type information is not preserved in the resulting JavaScript code. Therefore, the element created using JSX will have a static type of JSX.Element, which does not include specific type information about the component or its props. |
Hi @MeenuyD
My issue is before the transpilation. I am talking about the infered type given by vscode during development. |
when using JSX syntax, the inferred type for the component may appear as JSX. Element, which is a generic type that represents any JSX element. This happens because the type inference for JSX elements is not as powerful as the type inference for function calls or React.createElement |
Thanks for the clarification MeenuyD. So what we need is to tell the JSX syntax to use a given function behind (here If no such feature exists, then we should at least start to open an issue to give this idea to repo responsible for the JSX generated types, which I don't know how to find it. Any idea how we can make this going further? |
Closing since this is an issue caused by https://github.com/microsoft/typescript itself. There's probably an issue on their side already. But it generally falls under the category of "no constrained/generic JSX children" in typescript. |
Thanks @eps1lon for the repo. |
React version: 18.2.0
Steps To Reproduce
createElement()
.createElement()
has kept a generic ref to the component and extendsFunctionComponentElement
in this case, while the one using JSX hasn't and is statically justJSX.element
.Link to code example:
https://codesandbox.io/s/jsx-element-loses-types-6nmh3m?file=/src/App.tsx
The current behavior
The element created by JSX don't keep any reference to the component or its props types.
The expected behavior
The element created by JSX should keep any reference to the component or its props types as
React.createElement()
does.The text was updated successfully, but these errors were encountered: