next.js: 'SomeComponent' cannot be used as a JSX component.
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: win32
Arch: x64
Version: Windows 10 Pro
Binaries:
Node: 18.12.0
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant packages:
next: 13.0.1-canary.4
eslint-config-next: 13.0.0
react: 18.2.0
react-dom: 18.2.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
Any components are producing IDE error but builds successfully.
'SomeComponent' cannot be used as a JSX component.
Its return type 'Promise<Element>' is not a valid JSX element.
Type 'Promise<Element>' is missing the following properties from type 'ReactElement<any, any>': type, props, key `ts(2786)`
Expected Behavior
probably not give an error when using async components
Link to reproduction
no link
To Reproduce
// ~/component/SomeComponent.tsx
export default async function SomeComponent() {
let categories: any = await getCategories()
return ...
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 94
- Comments: 108 (25 by maintainers)
Commits related to this issue
- Ignore TS bug https://github.com/vercel/next.js/issues/42292 — committed to manueljenni/manueljenni.com_nextjs by manueljenni 2 years ago
- Add temporary workaround for async JSX https://github.com/vercel/next.js/issues/42292 — committed to glennreyes/glennreyes.com by glennreyes a year ago
- app user commitments list: fetch commitments and display them - Used await on the component because of this issue https://github.com/vercel/next.js/issues/42292 — committed to olivorocksrotated/olivo by lean1190 a year ago
- app user commitments list: fetch commitments and display them - Used await on the component because of this issue https://github.com/vercel/next.js/issues/42292 — committed to olivorocksrotated/olivo by lean1190 a year ago
- update some packages and fix resolution Ref: https://github.com/vercel/next.js/issues/42292#issuecomment-1555975533 — committed to isoppp/my-nextjs-starter by isoppp a year ago
- fix: Address component errors - 'SomeComponent' cannot be used as a JSX component - See https://github.com/vercel/next.js/issues/42292 — committed to ShawnToubeau/mapit by ShawnToubeau a year ago
I think a fix is upcoming, but pretty much the issue is that TypeScript doesn’t understand async components… It always expects JSX as child, not
Promise<JSX>.Many are type casting the component and exporting that type casted version… I think it might be better to do:
When the types are fixed, the
@ts-expect-errordirective will complain that it is unused…This fix might come from the next typescript plugin I think, not sure.
Finally worked:
No error. 😂
Just added a few notes about this workaround to the beta docs as well, thank you!
This particular error is hardcoded in TypeScript. The React team is working with the TypeScript team to resolve this. For now you can use the recommendation above 👍
You can remove all ts-expect-error related to this error now. If you’re using vscode make sure you’re using local version of it.

I’m on Next.js 13 and I’ve found two workarounds:
First one is using a const:
Second one is using
{/* @ts-expect-error Server Component */}:I’ve confirmed this is working! 🎉
To use an
asyncServer Component with TypeScript, ensure you are using TypeScript5.1.3or higher and@types/react18.2.8or higher, and select the correct TypeScript version inside of VSCode.If you are still seeing issues, please try restarting VSCode or clearing
node_modules.Issue persisted.
Afaik, this is the PR that will fix it https://github.com/DefinitelyTyped/DefinitelyTyped/pull/65135 in combination with TypeScript 5.1. It should be released in a few days, with the official release of 5.1.2 TS coming tomorrow. I’ve managed to make it work locally by patching this PR content onto older version of react types - https://github.com/pawelblaszczyk5/coloruiz/blob/main/patches/%40types__react%4018.2.7.patch
Just upgraded to
next 13.4.0,@types/react 18.2.5@types/react-dom 18.2.3in https://github.com/kachkaev/website/pull/32 – still seeing this error:Upgrading
typescript 5.1.0-betatotypescript 5.1.0-dev.20230502(latest available build) did not help.Is there a public issue/PR on the TypeScript repo where that work can be followed?
Didn’t work for me.
I upgraded my package types: “@types/node”: “18.14.2”, “@types/react”: “18.0.28”, “@types/react-dom”: “18.0.11”, and it was resolved!
The issue may have been introduced to your project via some other packages’ peer dependency.
If you have a .tsconfig file, Add following to compilerOptions:
“paths”: { “react”: [ “./node_modules/@types/react” ] }
The
{/* @ts-expect-error Server Component */}get’s rid of the typescript error as mentioned by icyJoseph. However, This produces a differentServer Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.. Unsure If I’m doing something wrong. I’m using a RSC in the/pagesdirectory which isPromise<JSX.Element>and returns a list. The data to it is passed via gssp and props.My guess is that the Promise is not resolved at the time of rendering. Hence, the error. If this is so, any workarounds until it is fixed.
Looks like
typescript@5.1.3(release notes) and@types/react@18.2.8were released! 🎉Maybe someone can confirm that this works out of the box now, and the issue can be closed?
Edit: Oh, @pawelblaszczyk5 thanks for confirming!
Now that https://github.com/microsoft/TypeScript/pull/51328 is merged and available in
typescript@5.1.0-betaare there any additional changes needed for this to work without the{/* @ts-expect-error Server Component */}hack?Example
I upgraded typescript to the beta and still see the error:
didn’t work either 😦
I’m using NextJS 13, the error still persists if you pass async server components. But
{/* @ts-expect-error Server Component */}makes it work, would love to have a non-hacky way to approach this.@icyJoseph
{/* @ts-expect-error Server Component */}worked for now.Thanks
I have confirmed this is now working with
@types/react18.2.8typescript5.1.3The PR got merged and It’s working correctly with
@types/react@18.2.8and latest TypeScript version 🥳@zackdotcomputer I tried that, but it didn’t change anything.
@srigi Another variation using the function keyword with a named export, note i have named both the variable and function the same to minimize refactoring when this TS issue is resolved.
@sami616 genius, thanks! It can be simplified to…
of if you prefer an even shorter classic function definition:
I just ran into this too–the recommendation to use
as unknown as () => JSX.Elementis what worked for me, here’s what I was doing:some page
@/util/HasPermission.tsxI was happy to see at least here that in my call at the bottom, setting
(props: Props)continued to let typescript understand what the required props were.The problem with
{/* @ts-expect-error Server Component */}is that it will silence other typing errors.I updated the Typescript version in my project to 5.1.3 as well as @types/react to 18.2.8 suggested by @pawelblaszczyk5 but unfortunately the error still shows up in my case.
It’d be also nice to adjust this docs part https://nextjs.org/docs/app/building-your-application/data-fetching/fetching#async-and-await-in-server-components:~:text=Async Server Component TypeScript Error
Starts at 18.2.4, 18.2.3 and lower are fine.
I am also having this issue. Upgrading didn’t help because type
ElementTypeis not available in new packages?This is what nextjs doc says: https://nextjs.org/docs/app/building-your-application/configuring/typescript)
I’ve done all the steps, yet still with the error of ‘xxx cannot be used as a JSX component’.
Yeah I’m still getting the issue
Module '"react"' has no exported member 'cache'.I faced a similar issue after upgrading
@types/reactfrom18.2.0to18.2.6alongside@types/react-domfrom18.2.1to18.2.4.TL;DR: Try deduplicating
@types/reactin your lockfile, or remove and re-install@types/react-dom.The underlying issue appeared to be that
@types/react-domlists@types/react@*as a dependency, so both@types/react@18.2.6and@types/react-dom > @types/react@18.2.0ended up being installed after the upgrade. This results in some sort of a type conflict and produces the error above.@icyJoseph {/* @ts-expect-error Server Component */} saved me thanks.
{/* @ts-expect-error Server Component */}Saved my day!
I worked around this one by writing a HOC. At least it wont shadow typing errors, keeps argument names and it’s less verbose to write compare to casting it multiple times.
Based on https://twitter.com/sebsilbermann/status/1664356039876124672 I created a new Next.js application using create-next-app and was able to verify it works now.
pnpm create next-app --typescript typescript-async-componentapp/page.tsx:And added:
Great work @eps1lon!
The latest version of React (specifically for server components) supports async functions as JSX, TypeScript just wasn’t up to date with that yet.
This guide along with this link fixed my issue - https://nextjs.org/docs/app/building-your-application/configuring/typescript
Similar solution here. In my case:
npm i> Developer: Reload windowAnd VSCode asked me to use the local typescript version instead of the global one. That did the trick.
The reoccurance of this error with
@types/react18.2.4+ is because of a mismatch in the typedefs between the one used internally by Next and the one your app uses if you are using Typescript 5. The types package added a TS5 specific typedef in 18.2.4 that isn’t present previously. If you downgrade to TS4 or the types package, it will fix the issue, but you can also force Next’s types to use the latest version inpackage.jsonto make the issue go away:(Edit: I see they’ve already updated the dependency in Next so the next release should also fix this.)
Same here, I am passing a SSC as a child to a CSC. Both are wrapped in a SSC as mentioned in the Nextjs Docs. Still I am getting the error “TS2786: ‘TopicCardContent’ cannot be used as a JSX component.”
<TopicCard side={"left"}><TopicCardContent /></TopicCard>I solved this problem with following steps (if you are using VSCode):
Source: https://stackoverflow.com/a/65151679/3317931
Latest Typescript version has fixed this error.
Reloading the window in VSCode might help. Worked from me. Also make sure @pawelblaszczyk5 suggestion about the workspace is set correctly.
@professorhaseeb or @leerob maybe the issue can be closed now?
doesn’t for me at least when props are passed downedit: this did it, thanks
export default OccurrencesTable as unknown as (props: OccurrencesTableProps) => JSX.Element;👍@hiendaovinh
Here is a wonky workaround which gets around the issue without losing all type safety for your incoming props. 👇🏼
Don’t know why, but I am using
appdirectory, but I can still see,Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.issue while renderingTo my understanding server components are only available in the new app/ folder not pages/ so that would be the cause of your issue.
More background on the
@emotion/reactproblem (fixed in@emotion/react@11.11.1):Promise<React.ReactNode>seems to work just fine with latest types: Playground LinkThe only bug we need to fix in types/react is that async is only for experimental when it’s already available in canary.
Oh, that’s strange, I’ve checked it now and I see this issue as well. Maybe it’s because the underlying type is using
PromiseLikeinstead of nativePromiseinterface 🤔 Because it seems like it gets stripped in proposed typeI personally don’t use explicit return types so I don’t have an idea for a good solution here, sorry. Though, it’s not related to this issue tbh, it’s just a typing problem
@timneutkens i am still seeing an error with an explicit
Promise<ReactNode>return type annotation: “Type is referenced directly or indirectly in the fulfillment callback of its own ‘then’ method.ts(1062)”see https://github.com/vercel/next.js/issues/42292#issuecomment-1575583207
Didn’t expect this to work, but nuking node_modules and pnpm-lock.yarn and re-installing actually fixed the issue.
I just confirmed that upgrading typescript packages and selecting the VSCode TS version fixed the issue. 🙌
Unfortunately I can not do that. We are generating code for user to copy and paste into their app. We dont know which version of TS/react types will they have. If we remove ts-expect-error that will fail old versions. If we keep it we fail new versions. If we change it to ts-ignore we basically disable type checking. All bad options 😦
https://github.com/vercel/next.js/issues/42292#issuecomment-1491127192
as unknown as () => JSX.Elementworks like a charm, thank you!Although
{/* @ts-expect-error Server Component */}also works, the earlier workaround remains the best.This works great. No need to fuss around with same pattern on every ServerComponent
Worth to note: If you put the
{/* @ts-expect-error Server Component */}before the async JSX element, it will also ignore any other (unrelated) errors you might have. For example passing a prop but with a wrong type.You only use async component inside app folder and server component.
Ah Yes. I was not aware of it. In a sandbox I was able to reproduce the above error which I was expecting in
/pagesdirectory. Then I switched to beta/appdirectory and it worked charmingly. I now understand why it happened. Thanks @dantman!