Why does useContext report “Type 'bigint' is not assignable to type 'ReactNode'”?

February 26, 2025

I wanted to display a notification whenever an API request returned an error. Originally my application, PuppyFlow, managed its own notifications. Later I moved the notification component into a neighboring local package, PuppyUI, and had the application import WarnsContext from it. The line that read the messages hadn't changed, but TypeScript reported an error I wasn't expecting.1

const { warns, setWarns } = useContext(WarnsContext);

Type 'bigint' is not assignable to type 'ReactNode'.1

warns contains messages and timestamps, and setWarns updates that list. bigint is JavaScript's type for large integers. How did reading a list of messages lead to a check involving large integers?

Reading back from the last line, why do we find children?

Start with what WarnsContext does. React's Context lets an outer component pass a value to its descendants without passing props through every component in between. My notification component used a Provider to supply that value. Here's a simplified version of what I wrote.1

<WarnsContext.Provider value={{ warns, setWarns }}>
  {children}
</WarnsContext.Provider>

There are two distinct things here. The value is the message list and update function that a descendant receives when it calls useContext(WarnsContext). The children are the UI nested between the two tags, such as the whole editor. React's type declarations use ReactNode to describe what can go there: for example, some text, a number, or a button.

Expanding the error in the editor reveals the first line: No overload matches this call. TypeScript checked the function's supported call signatures, and the argument didn't fit any of them. Shortening the paths and omitting repeated parts of one diagnostic shows where it found the difference.1

PuppyUI/node_modules/@types/react 的 Context 不能传给应用的 useContext
  两边的 Provider 类型不兼容
    两边对 children 的要求不兼容
      应用的 ReactNode 不能赋给 PuppyUI 的 ReactNode
        Type 'bigint' is not assignable to type 'ReactNode'.

TypeScript checks whether the members of the types it's comparing are compatible. If one side permits a large integer as content and the other can't accept it, the compiler can cite bigint as a counterexample even though I've never passed one. Reading back up the message points to the PuppyUI/node_modules/@types/react path above. Which React declarations were the application and the component package each reading?2

Why did someone else's createPortal report the same error?

I found a Next.js discussion where someone passed children to createPortal to display that UI somewhere else on the page. Their error also compared two copies of ReactNode, one from @types/react-dom/node_modules/@types/react. A reply said that wrapping children in <>…</> passed the check. That wraps the content in a React element and changes the type of the expression passed to the function.3

I tried the same createPortal line in my notification component and hit two more obvious questions: where was createPortal defined, and which element was modalElem? The post had a modal container. My code only needed to pass notification state to descendants. The component I later saved removed that experiment and continued rendering children inside the Provider.1

In the same discussion, eps1lon asked a question that fit my case better: “Does npm ls @types/react list a single version or multiple?”4 The react package provides the code that actually runs. The @types/react package tells TypeScript how to check it. If your application installs a neighboring component package through file:../PuppyUI, run the following command in both the application directory and the component package directory. Compare the installed versions in the output to the paths in the full diagnostic. Declarations in two different directories can still pass if the definitions the compiler sees are compatible.5

npm ls react @types/react

In the end, did React change, or did its types change?

The installation records from the evening of February 25 do show a conflict in React itself. When npm tried to resolve react@19.0.0, the project's editor component, react-ace@12, declared support only up through React 18. That installation failed after I'd posted the useContext error. Between the files saved at 19:28 and 19:29, PuppyUI's react remained at 18.3.1; what changed was @types/react, from 18.3.1 to 18.3.12. The latter matched the types version recorded in the application's lockfile.6

I also left useContext(WarnsContext) as any in a usage example for the component package. The as any assertion relaxes checks on the return value, so the code can then access warns and setWarns. The original error occurred when passing the argument WarnsContext into the function. An assertion on its return value can't resolve that step.6

The application code committed on February 26 took a shorter route. It imported the application's internal WarnsContext directly, kept the ordinary useContext(WarnsContext) call, and left the import from puppyui commented out. Later I checked the original call in isolation with the saved application dependencies and PuppyUI source. Both sides had react@18.3.1 and @types/react@18.3.12, and it passed without as any. The commit shows what actually shipped at the time. The later test verifies that with aligned versions this call can pass type checking across the package boundary. I found no record establishing which installation produced the first successful build of the whole project that evening.6

References

Footnotes

  1. CaiBorui. (2025, February 25). Fixing TypeScript JSX and Type Errors [Private development conversation, original diagnostics and selected code in messages 3 and 5, follow-up questions in messages 7 and 9]. Cursor. See also the component saved that day at PuppyUI/core/components/PuppyWarns/WarnMessageContext.tsx. ↩ ↩2 ↩3 ↩4 ↩5

  2. Microsoft. (n.d.). Type compatibility. TypeScript Handbook. Starting out. ↩

  3. waikoo. (2024, April 18). Type 'bigint' is not assignable to ReactNode – help [Discussion #64753, summary and portal example]. GitHub. Original error. vilindberg. (2024, April 26). Reply wrapping children in a Fragment. ↩

  4. eps1lon. (2024, September 17). Reply to “Type 'bigint' is not assignable to ReactNode – help” [Reply, paragraph 1]. GitHub. Original comment. ↩

  5. npm. (n.d.). npm ls [npm CLI v10 documentation]. Description. ↩

  6. CaiBorui. (2025, February 25–26). PuppyUI and PuppyFlow dependency manifests, notification component examples, and application call site [Local project records]. Cursor file history, npm installation report, and Git. PuppyUI's package.json was saved at 19:28:06 and 19:29:18. For the call in application commit d04e3d4f, see PuppyFlow/app/components/hooks/useJsonConstructUtils.ts. On 2026-12-03 I rechecked the retained dependencies and README. I used the application's TypeScript compiler to load its Next.js type environment and the component package's Context source, then checked the call in isolation in strict mode. ↩ ↩2 ↩3

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-02-26). Why does useContext report “Type 'bigint' is not assignable to type 'ReactNode'”?. https://caiborui.com/blog/2025-02-26-zh-usecontext-reactnode-bigint-type-mismatch-entrans

@misc{borui2025-9e505429,
  author = {borui},
  title = {{Why does useContext report “Type 'bigint' is not assignable to type 'ReactNode'”?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-02-26-zh-usecontext-reactnode-bigint-type-mismatch-entrans}
}