ts-proto icon indicating copy to clipboard operation
ts-proto copied to clipboard

Write null instead of undefined

Open tomasweigenast opened this issue 1 year ago • 2 comments

A good option to implement can be the possibility to compare with null instead undefined. This is useful when dealing with Firestore, which skips undefined properties and can generate issues when sorting.

tomasweigenast avatar Jul 08 '23 15:07 tomasweigenast

I was just about to open an issue and I saw this!

This is a must-have in my opinion. In my company we all work with null types since undefined can thrown in some cases, Working with null is more convenient to know if it was the developer's intention or unknown.

From my searching, there have been multiple issues about it, what do you say @stephenh :p

0xRapid avatar Aug 13 '23 15:08 0xRapid

Is the idea that if Author is a message, and Author.address is an unset child message, that console.log(author.address) would print null?

Fwiw normally in non-protobuf/JSON APIs, I like to use child: null to be "unset the child" and child: undefined (or just omitted) to mean "leave child alone", so I get the intuition for "null more intentionally means unset/empty".

That said, protobuf doesn't have a way to differentiate these two on the wire, so that null-is-unset / undefined-is-omit convention is (unfortunately imo) not applicable to protobuf APIs.

Granted, Firestore and other "null is just our preferred convention" is fair, but personally not something I'll work on.

If you'd like to submit a PR, and the PR is not huge, I'd consider accepting it as yet-another-flag...that said, I'm worried such a fundamental change is going to be a lot of complications to the codebase, but who knows, maybe not.

stephenh avatar Aug 13 '23 17:08 stephenh

:tada: This issue has been resolved in version 1.171.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

stephenh avatar Mar 30 '24 23:03 stephenh