Getting error: error TS2786: 'form.Field' cannot be used as a JSX component. in existing project.
Describe the bug
> npm run typecheck
> @<REDACTED>/[email protected] typecheck
> tsc --noEmit
src/routes/<REDACTED>/index.lazy.tsx:104:10 - error TS2786: 'form.Field' cannot be used as a JSX component.
Its return type 'ReactNode' is not a valid JSX element.
Type 'undefined' is not assignable to type 'Element | null'.
104 <form.Field name="displayTableName">
~~~~~~~~~~
src/routes/<REDACTED>/index.lazy.tsx:127:10 - error TS2786: 'form.Field' cannot be used as a JSX component.
Its return type 'ReactNode' is not a valid JSX element.
127 <form.Field name="mdDesc">
~~~~~~~~~~
src/routes/<REDACTED>/index.lazy.tsx:150:10 - error TS2786: 'form.Subscribe' cannot be used as a JSX component.
Its return type 'ReactNode' is not a valid JSX element.
150 <form.Subscribe
~~~~~~~~~~~~~~
The error is also shown in my editor.
Your minimal, reproducible example
https://github.com/LumaKernel/tanstack-form-issue-1476
Steps to reproduce
- Clone above.
- npm ci
- npm run typecheck (Or just open
src/App.tsxin your editor)
Expected behavior
Built without errors.
How often does this bug happen?
Every time.
Screenshots or Videos
Platform
- Node: v22.14.0
- npm: 10.5.0
TanStack Form adapter
react-form
TanStack Form version
@tanstack/[email protected]
TypeScript version
Additional context
I'm still trying to minimize how can I get this error in plain project.
I tried to fix versions of react, typescript, @types/react, @types/react-dom. And using the same tsconfig.json. But still it's succeeding and not reproduced in plain project.
I believe I can resolve this problem in next hours, but I'm leaving it for future who searched the keywords for this problem.
EDIT: It's reproduced.
Ah, sorry, it can be resolved by placing the latest tsconfig.json + tsconfig.app.json + tsconfig.node.json which is made by vite create to current existing project. But it's only for tsc, and still error for the editor.
↑ It wasn't actually resolving the problem. Even if I put type error explicitly, it succeeded.
Ok, now it's reproduced https://github.com/LumaKernel/tanstack-form-issue-1476
Now I confirmed it's happening when switching @types/react + @types/react-dom from 19 to 18.
Just clarification, v18 is listed in peerDependencies. https://github.com/TanStack/form/blob/2a52a8070dea6f4b012f8d2281a65b6d481145dc/packages/react-form/package.json#L102
Unfortunately, we can't easily switch to v19, so are still finding the way to resolve this.
It's likely because of this type here: https://github.com/TanStack/form/blob/2a52a8070dea6f4b012f8d2281a65b6d481145dc/packages/react-form/src/useField.tsx#L421
Same issue with form.Subscribe:
tsserver: 'form.Subscribe' cannot be used as a JSX component.
Its return type 'ReactNode' is not a valid JSX element.
Type 'undefined' is not assignable to type 'Element | null'.
I think it is the same issue as https://github.com/TanStack/form/issues/1302
To workaround this problem, I thought some ideas, and finally it's easiest:
Original:
<form.Field
name="name"
>{
(field) => (
<div>...</div>
)
}</form.Field>
To be rewritten as:
{form.Field({
name: "name",
children: (field) => (
<div>...</div>
),
})}
And I can confirm it's working and error is gone.
But please not that, the way of this would be out of scope of React and its optimization, and must not be recommended.
Your project has "@types/react": "^18.2.7". Upgrading the types to ^18.3.21 seems to fix the problem. Is that types version compatible with React 18?
Your project has
"@types/react": "^18.2.7". Upgrading the types to^18.3.21seems to fix the problem. Is that types version compatible with React 18?
Thanks. It seems to resolve my case completely, and the migration could be done with less efforts.