Skip to content

undefined instead of default for optional fields ? #1572

@sloonz

Description

@sloonz

protobuf.js version: 6.10.2

Currently, to test if an optional field has been provided, you have to use message.hasOwnProperty("optField") because message.optField will return the default value (0 for numbers, "" for strings) if not present in the message.

While this is in line with idiomatic protobuf, it is completely out of line with idiomatic javascript where unset optional field would just return undefined when accessed.

My suggestion is to add a pbjs option (for example --no-optional-defaults) for people like me that prefer the javascript way, that would set Message.prototype.optField to undefined instead of the default value for the type.

(for the record, I’m willing to work on this feature if you’re fine with integrating it)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions