Skip to content

Conversation

@minggangw
Copy link
Member

Currently, the type of callback for a subscription is defined as:

type SubscriptionCallback<T extends TypeClass<MessageTypeClassName>> =
  (message: MessageType<T> | Buffer) => void

which causes a type error when pass a callback either for a normal message or raw message.

This patch provides two types of callback for a subscription, which are:

  1. For a normal message
type SubscriptionCallback<T extends TypeClass<MessageTypeClassName>> =
  (message: MessageType<T>) => void;
  1. For a raw message:
type SubscriptionWithRawMessageCallback =
  (message: Buffer) => void;

After this patch, we can use the correct callback type for a subscription, like:

subscription = node.createSubscription(
  TYPE_CLASS,
  TOPIC,
  { isRaw: true },
  (message: Buffer) => {
    const receivedRawMessage = message;
  }
);

Fix: #1013

@minggangw minggangw requested a review from Copilot February 8, 2025 06:19
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

test/types/main.ts:250

  • The comment should be updated to maintain consistency with other comments. Suggested change: // $ExpectType Subscription
// $ExpectType Subscription receiving raw message

@minggangw minggangw merged commit 3ffc543 into RobotWebTools:develop Feb 8, 2025
6 checks passed
minggangw added a commit that referenced this pull request Feb 8, 2025
Currently, the type of callback for a subscription is defined as:
```ts
type SubscriptionCallback<T extends TypeClass<MessageTypeClassName>> =
  (message: MessageType<T> | Buffer) => void
```
which causes a type error when pass a callback either for a normal message or raw message.

This patch provides two types of callback for a subscription, which are:

1. For a normal message
```ts
type SubscriptionCallback<T extends TypeClass<MessageTypeClassName>> =
  (message: MessageType<T>) => void;
``` 

2. For a raw message:
```ts
type SubscriptionWithRawMessageCallback =
  (message: Buffer) => void;
```

After this patch, we can use the correct callback type for a subscription, like:
```ts
subscription = node.createSubscription(
  TYPE_CLASS,
  TOPIC,
  { isRaw: true },
  (message: Buffer) => {
    const receivedRawMessage = message;
  }
);
```

Fix: #1013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typescript: message type on SubscriptionCallback<T> based on isRaw

1 participant