thesayyn / protoc-gen-ts

Compile protocol buffer messages to TypeScript.
MIT License
360 stars 74 forks source link

Presence clear methods #164

Closed Santalov closed 1 year ago

Santalov commented 2 years ago

This change only affects the users of the tsconfig option strictNullChecks, others can completely ignore it.

In typescript types of getters and setters match exactly. This is ok, cos when a field always has value (!canHaveNullValue), assigning undefined to it makes no sense: the getter of the field will always return some non-null value, so the code bellow will print 0.

let x = undefined;
msg.int64 = x; // msg.int is a proto3 int64 field
x = msg.int64;
console.log(x); // will print 0, might be confusing

The implication is that you cannot assign undefined to a field, that always returns non-null value (if you are using strictNullChecks:true). This is a problem when dealing with optional fields, so clear_* methods were introduced. The plugin generates them for all fields, that had has_* method generated. Those methods are described in the official doc https://github.com/protocolbuffers/protobuf/blob/main/docs/field_presence.md

thesayyn commented 1 year ago

Closing as there's been no activity on this.