Skip to content

Can't get typing for output of toJSON #15479

@lobabob

Description

@lobabob

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.15.1

Node.js version

22.13.1

MongoDB version

8.0.4

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

No response

Issue

The toJSON method returns any for the output type. I've attempted to specify the output type of the transform function, but due to the function signatures defined in document.d.ts, it gets widened to any making it pointless. Is there some method I'm missing on how to do this? Everything I've been able to find online about this (and what I've seen after poking around mongoose internals) seems to indicate there's no way to do this currently.

If it isn't possible, would it make sense to update the toJSON return type to be dynamic? It might work if the transform function required a typing to be supplied (using any as a fallback to avoid breaking backwards compat) with generics and then toJSON would just use whatever the return type of the transform function is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    helpThis issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessaryhelp wantedtypescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions