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)
protobuf.js version: 6.10.2
Currently, to test if an optional field has been provided, you have to use
message.hasOwnProperty("optField")becausemessage.optFieldwill 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
undefinedwhen 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 setMessage.prototype.optFieldtoundefinedinstead 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)