improbable-eng / ts-protoc-gen

Protocol Buffers Compiler (protoc) plugin for TypeScript and gRPC-Web.
Apache License 2.0
1.36k stars 173 forks source link

Fix TypeScript annotations for setters #289

Open roboslone opened 2 years ago

roboslone commented 2 years ago

Currently there's no way to chain setters, because they don't return anything. It would be convenient to have setters return the object they're called on:

// Current
const message = new Message()
message.setName('name')
message.setCount(1)

const subMessage = new SubMessage()
subMessage.setName('sub')
message.setSubMessage(subMessage)

// Proposed
const message = new Message()
    .setName('name')
    .setCount(1)
    .setSubMessage(new SubMessage().setName('sub'))
roboslone commented 2 years ago

UPD I've checked generated JS code and it seems that JS actually returns this. TypeScript annotations have void though.

/**
 * @param {string} value
 * @return {!proto.MyMessage} returns this
 */
proto.MyMessage.prototype.setSymbol = function(value) {
  return jspb.Message.setProto3StringField(this, 1, value);
};

export class MyMessage extends jspb.Message {
  setSymbol(value: string): void;
}
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

roboslone commented 2 years ago

It's still relevant.

reddaly commented 2 years ago

The fix should be relatively easy. The file to modify is here: https://github.com/improbable-eng/ts-protoc-gen/blob/master/src/ts/message.ts