Skip to content

JSON.stringify should disallow BigInt value #63333

Closed as not planned
Closed as not planned

Description

@wyattscarpenter

⚙ Compilation target

es2023

⚙ Library

lib.es5.d.ts

Missing / Incorrect Definition

JSON.stringify is correctly marked with @throws {TypeError} If a circular reference or a BigInt value is found., but it types its input value parameter as any. That type could be improved so that BigInt can't even be passed into the function (turning one runtime error into a compiletime error).

Perhaps even every type that contains a BigInt can and should be excluded. I'm not very good at TypeScript but that might be possible.

Note that if a replacer function is specified, this complicates the situation somewhat.

This issue is distinct from the perennial proposal to correctly treat undefined in JSON.stringify (eg #18879). Hopefully, the false-positive impact of this one should be much lower, and thus it can be accepted.

Sample Code

//These are runtime errors, but TypeScript doesn't complain.
//"Uncaught TypeError: Do not know how to serialize a BigInt"
JSON.stringify(1n);
JSON.stringify([1n]);

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/BigInt_not_serializable

https://tc39.es/ecma262/multipage/structured-data.html#sec-json.stringify

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Activity

  1. nmain commented on Apr 2, 2026

    @nmain

    What type would the argument to JSON.stringify have? If it tried to list only things that were guaranteed to be JSON stringifyable, that would be #1897. If you wanted Exclude<unknown, bigint>, that would be #4196.

  2. added
    Duplicate
    on Apr 2, 2026
  3. typescript-bot commented on Apr 5, 2026

    @typescript-bot
    Contributor

    This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  4. locked as resolved and limited conversation to collaborators on Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions