-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Improve error message for invalid return type of JSX component #32702
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
Improve error message for invalid return type of JSX component #32702
Conversation
fecfd5a
to
64b5bed
Compare
@@ -1,9 +1,11 @@ | |||
tests/cases/conformance/jsx/inline/index.tsx(5,1): error TS2741: Property '__predomBrand' is missing in type 'import("tests/cases/conformance/jsx/inline/renderer").dom.JSX.Element' but required in type 'import("tests/cases/conformance/jsx/inline/renderer2").predom.JSX.Element'. | |||
tests/cases/conformance/jsx/inline/index.tsx(21,40): error TS2322: Type 'import("tests/cases/conformance/jsx/inline/renderer").dom.JSX.Element' is not assignable to type 'import("tests/cases/conformance/jsx/inline/renderer2").predom.JSX.Element'. | |||
tests/cases/conformance/jsx/inline/index.tsx(21,40): error TS2605: JSX element type 'MyClass' is not a constructor function for JSX elements. | |||
Property '__domBrand' is missing in type 'MyClass' but required in type 'ElementClass'. | |||
tests/cases/conformance/jsx/inline/index.tsx(21,41): error TS2774: This expression cannot be used as a JSX component. |
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.
Can we go back to naming the expression? For example, 'MyClass' cannot be used as a JSX component.
?
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.
Ah,
The original suggestion in #30850 suggests to include
{component name}
in the error message, but I found it hard to stringify JSX tag expressions for use by error messages. Instead I usedThis expression
.
Maybe another reviewer can help.
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.
@DanielRosenwasser Sorry, my investigation wasn't enough. I found a utility function which can do it.
I updated the PR to include component name in error messages.
e50046c
to
7cf4d9a
Compare
7cf4d9a
to
2957ec7
Compare
Fixes #30850.
This PR improves the error messages shown when the return/instance type of the function/class used as a JSX component is not assignable to
JSX.Element
orJSX.ElementClass
.I thought the problem is not specific to
void
; referring toconstructor function
is always misleading.So this PR fully replaces the current message with new ones.
The new error messages refer to
return type
,instance type
orelement type
depending on what call signatures the component has.Also this PR changes the span of the error from whole JSX element to only its tag name so the actual cause is clearer.
Example
source code:
current behavior (TS3.5.3):
new behavior:
Alternative Option
I chose to separate the new error messages to two parts: one is 'ComponentName' cannot be used as a JSX component.
and the other is
Its return type '{0}' is not a valid JSX element.or similar. Another option is to merge them into one so the new message is
'ComponentName' cannot be used as a JSX component because its return type '{0}' is not a valid JSX element.`I chose the first option for two reasons. One is that in this way we get the same top-level error message for all the three cases.
The other reason is that the merged message, especially the part before
'{0}'
, is too long. The cause of the error (the return type{0}
in this case) should be reachable as easily as possible. This is achieved by chaining two messages as done in this PR.