From f68f7f6337d448a9a37e0df18d0c493a6b3093fb Mon Sep 17 00:00:00 2001 From: Minggang Wang Date: Sat, 8 Feb 2025 13:34:34 +0800 Subject: [PATCH 1/2] [TypeScript] Provide two types of callback for a subscription --- test/types/main.ts | 20 ++++++++++++++++++++ types/node.d.ts | 2 +- types/subscription.d.ts | 18 +++++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/test/types/main.ts b/test/types/main.ts index 40d3f4ba..21b1efd4 100644 --- a/test/types/main.ts +++ b/test/types/main.ts @@ -237,6 +237,26 @@ subscription = node.createSubscription( (msg) => {} ); +// $ExpectType Subscription +subscription = node.createSubscription( + TYPE_CLASS, + TOPIC, + { isRaw: false }, + (message: rclnodejs.std_msgs.msg.String) => { + const receivedMessage: rclnodejs.std_msgs.msg.String = message; + } +); + +// $ExpectType Subscription receiving raw message +subscription = node.createSubscription( + TYPE_CLASS, + TOPIC, + { isRaw: true }, + (message: Buffer) => { + const receivedRawMessage: Buffer = message; + } +); + // $ExpectType string subscription.topic; diff --git a/types/node.d.ts b/types/node.d.ts index 0a9cd130..fd3d3177 100644 --- a/types/node.d.ts +++ b/types/node.d.ts @@ -324,7 +324,7 @@ declare module 'rclnodejs' { typeClass: T, topic: string, options: Options, - callback: SubscriptionCallback + callback: SubscriptionCallback | SubscriptionWithRawMessageCallback ): Subscription; /** diff --git a/types/subscription.d.ts b/types/subscription.d.ts index 9e93be2c..a0a41c9b 100644 --- a/types/subscription.d.ts +++ b/types/subscription.d.ts @@ -13,7 +13,23 @@ declare module 'rclnodejs' { */ type SubscriptionCallback> = // * @param message - The published message - (message: MessageType | Buffer) => void; + (message: MessageType) => void; + + /** + * A callback for receiving published raw messages. + * + * @param message - The published message. + * + * @remarks + * See {@link Node#createSubscription | Node.createSubscription} + * See {@link SubscriptionContentFilter} + * See {@link Node#createPublisher | Node.createPublisher} + * See {@link Publisher} + * See {@link Subscription} + */ + type SubscriptionWithRawMessageCallback = + // * @param message - The published raw message + (message: Buffer) => void; /** * A ROS Subscription for published messages on a topic. From 5074c4ff859a4d4d049f61d1d8c45f87218ef422 Mon Sep 17 00:00:00 2001 From: Minggang Wang Date: Sat, 8 Feb 2025 14:21:14 +0800 Subject: [PATCH 2/2] Remove type --- test/types/main.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/types/main.ts b/test/types/main.ts index 21b1efd4..ae1ccf97 100644 --- a/test/types/main.ts +++ b/test/types/main.ts @@ -243,7 +243,7 @@ subscription = node.createSubscription( TOPIC, { isRaw: false }, (message: rclnodejs.std_msgs.msg.String) => { - const receivedMessage: rclnodejs.std_msgs.msg.String = message; + const receivedMessage = message; } ); @@ -253,7 +253,7 @@ subscription = node.createSubscription( TOPIC, { isRaw: true }, (message: Buffer) => { - const receivedRawMessage: Buffer = message; + const receivedRawMessage = message; } );