From 9d29625c8ea7b3270fce0cd0eeb2c2dd0b5dfdc0 Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Wed, 25 Sep 2019 12:58:19 -0700 Subject: [PATCH] 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. --- types/google-protobuf/google-protobuf-tests.ts | 5 +++++ types/google-protobuf/index.d.ts | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/types/google-protobuf/google-protobuf-tests.ts b/types/google-protobuf/google-protobuf-tests.ts index 8104ba4680..1f1c69cc6c 100644 --- a/types/google-protobuf/google-protobuf-tests.ts +++ b/types/google-protobuf/google-protobuf-tests.ts @@ -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() diff --git a/types/google-protobuf/index.d.ts b/types/google-protobuf/index.d.ts index cc6de4f146..ee70b7618c 100644 --- a/types/google-protobuf/index.d.ts +++ b/types/google-protobuf/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/google/google-protobuf // Definitions by: Marcus Longmuir // Chaitanya Kamatham +// Austin Bonander // 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(msg: T): T; static cloneMessage(msg: T): T; static copyInto(fromMessage: Message, toMessage: Message): void;