hono icon indicating copy to clipboard operation
hono copied to clipboard

`hono/client` is overriding React types in a Next.js project

Open Yovach opened this issue 1 year ago • 5 comments

What version of Hono are you using?

4.0.0

What runtime/platform is your app running on?

Node.js 20

What steps can reproduce the bug?

If we're on a Next.js 14.1 project and we're importing hono as a dependency to use the hono/client, we can't use the following code :

import type { ComponentProps, ElementType, PropsWithChildren, ReactNode } from "react";

type Props = Omit<ComponentProps<"div">, "className"> & {
  as?: ElementType;
  children: ReactNode;
};

export function Container({ className, children, as }: Props) {
  const Tag = as || "div";
  return (
    <Tag>
      {children}
    </Tag>
  );
}

Because the following lines are overriding the JSX.ElementType type https://github.com/honojs/hono/blob/ca6f36511094522b84b6575d99bde4728bcea87c/src/jsx/base.ts#L13C1-L24C2

What is the expected behavior?

We shouldn't have the following errors :

  1. JSX element type 'Tag' does not have any construct or call signatures. [2604]
  2. 'Tag' cannot be used as a JSX component. Its type 'React.ElementType' is not a valid JSX element type. Type 'number' is not assignable to type 'ElementType'. [2786]

What do you see instead?

No response

Additional information

No response

Yovach avatar Feb 10 '24 20:02 Yovach

Hi @Yovach

How about putting the following line to the top of your file in the Next.js app?

/* @jsxImportSource react */

yusukebe avatar Feb 13 '24 02:02 yusukebe

Hi,

I was not able to fix this issue by putting

/* @jsxImportSource react */

I still have Type error: JSX element type 'Tag' does not have any construct or call signatures..

I also tried with "jsx": "react-jsx" in tsconfig.json but Next.js reverts to "preserve"

Yovach avatar Feb 14 '24 18:02 Yovach

@Yovach Could you share the minimal project to reproduce it?

yusukebe avatar Feb 14 '24 18:02 yusukebe

@Yovach Could you share the minimal project to reproduce it?

Sure, i'll attach a reproduction this weekend.

Yovach avatar Feb 14 '24 19:02 Yovach

@Yovach Thanks!

yusukebe avatar Feb 14 '24 19:02 yusukebe

Hi @yusukebe, Sorry for the long delay.

Here is a reproduction https://codesandbox.io/p/devbox/elegant-bogdan-ktsdc4

The error is in app/component/container.tsx

Yovach avatar Feb 18 '24 15:02 Yovach

Thanks! @Yovach

Hi @usualoma !

This error is caused by a conflict between React's JSX declaration and hono/jsx's JSX declaration. Is there any solution?

https://github.com/honojs/hono/blob/ca6f36511094522b84b6575d99bde4728bcea87c/src/jsx/base.ts#L13C1-L24C2

yusukebe avatar Feb 20 '24 09:02 yusukebe

Hi @yusukebe

How about #2248?

usualoma avatar Feb 20 '24 11:02 usualoma

Hi @usualoma @yusukebe, It seems to be fixed in my case. Thanks!

Yovach avatar Feb 20 '24 20:02 Yovach