form icon indicating copy to clipboard operation
form copied to clipboard

Getting error: error TS2786: 'form.Field' cannot be used as a JSX component. in existing project.

Open LumaKernel opened this issue 1 year ago • 12 comments

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.tsx in your editor)

Expected behavior

Built without errors.

How often does this bug happen?

Every time.

Screenshots or Videos

Image

Platform

  • Node: v22.14.0
  • npm: 10.5.0

TanStack Form adapter

react-form

TanStack Form version

@tanstack/[email protected]

TypeScript version

[email protected]

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.

LumaKernel avatar May 02 '25 08:05 LumaKernel

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.

LumaKernel avatar May 02 '25 08:05 LumaKernel

↑ It wasn't actually resolving the problem. Even if I put type error explicitly, it succeeded.

LumaKernel avatar May 02 '25 08:05 LumaKernel

Ok, now it's reproduced https://github.com/LumaKernel/tanstack-form-issue-1476

LumaKernel avatar May 02 '25 09:05 LumaKernel

Now I confirmed it's happening when switching @types/react + @types/react-dom from 19 to 18.

LumaKernel avatar May 02 '25 09:05 LumaKernel

Just clarification, v18 is listed in peerDependencies. https://github.com/TanStack/form/blob/2a52a8070dea6f4b012f8d2281a65b6d481145dc/packages/react-form/package.json#L102

LumaKernel avatar May 02 '25 09:05 LumaKernel

Unfortunately, we can't easily switch to v19, so are still finding the way to resolve this.

LumaKernel avatar May 02 '25 10:05 LumaKernel

It's likely because of this type here: https://github.com/TanStack/form/blob/2a52a8070dea6f4b012f8d2281a65b6d481145dc/packages/react-form/src/useField.tsx#L421

LeCarbonator avatar May 02 '25 14:05 LeCarbonator

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'.

ahmafi avatar May 03 '25 14:05 ahmafi

I think it is the same issue as https://github.com/TanStack/form/issues/1302

btravers avatar May 05 '25 14:05 btravers

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.

LumaKernel avatar May 07 '25 05:05 LumaKernel

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?

LeCarbonator avatar May 07 '25 06:05 LeCarbonator

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?

Thanks. It seems to resolve my case completely, and the migration could be done with less efforts.

LumaKernel avatar May 07 '25 08:05 LumaKernel