google-protobuf: adjust Message.clone[Message]() (#38476)

Returning [the polymorphic `this` type](https://www.typescriptlang.org/docs/handbook/advanced-types.html#polymorphic-this-types) ensures calling `.clone()` or `.cloneMessage()` on a subclass instance doesn't require a redundant cast.
This commit is contained in:
Austin Bonander 2019-09-25 12:58:19 -07:00 committed by Ben Lichtman
parent f966375bc7
commit 9d29625c8e
2 changed files with 8 additions and 2 deletions

View File

@ -534,3 +534,8 @@ class MySimple extends jspb.Message {
return jspb.Message.getField(this, 15) != null;
}
};
// ensures messages are cloneable without a redundant cast
const myMessage: MySimple = new MySimple();
const myClonedMessage: MySimple = myMessage.clone();
const myClonedMessage2: MySimple = myMessage.cloneMessage()

View File

@ -2,6 +2,7 @@
// Project: https://github.com/google/google-protobuf
// Definitions by: Marcus Longmuir <https://github.com/marcuslongmuir>
// Chaitanya Kamatham <https://github.com/kamthamc>
// Austin Bonander <https://github.com/abonander>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
type ByteSource = ArrayBuffer | Uint8Array | number[] | string;
@ -110,8 +111,8 @@ export abstract class Message {
static equals(m1: Message, m2: Message): boolean;
static compareExtensions(extension1: {}, extension2: {}): boolean;
static compareFields(field1: any, field2: any): boolean;
cloneMessage(): Message;
clone(): Message;
cloneMessage(): this;
clone(): this;
static clone<T extends Message>(msg: T): T;
static cloneMessage<T extends Message>(msg: T): T;
static copyInto(fromMessage: Message, toMessage: Message): void;