Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect typing of Struct.toJson() #773

Closed
CharlieMartell opened this issue Apr 4, 2024 · 3 comments
Closed

Incorrect typing of Struct.toJson() #773

CharlieMartell opened this issue Apr 4, 2024 · 3 comments

Comments

@CharlieMartell
Copy link

Describe the bug

Here: https://github.com/bufbuild/protobuf-es/blob/main/packages/protobuf/src/google/protobuf/struct_pb.ts#L72 we see that this function returns a JsonValue type but in the implementation it should return a JsonObject type. This means consuming applications need get type errors when doing anything like Object.entries(struct.toJson())

@timostamm
Copy link
Member

Also see: #508

@CharlieMartell
Copy link
Author

Well this isn't about a better typing to the JSON its just that the return value of this fn is just wrong. It only ever returns a JsonObject type

@timostamm
Copy link
Member

It is not wrong because a JsonObject is a JsonValue. Here's the definition:

export type JsonValue =
  | number
  | string
  | boolean
  | null
  | JsonObject
  | JsonValue[];

export type JsonObject = { [k: string]: JsonValue };

All messages actually return a JsonObject, the only exception are a handful of well-known types. We use JsonValue for both sides for symmetry, it was never intended to provide message-specific type information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants