Skip to content

How I can handle TS types error MQTT.js with RxJs #1893

Answered by AlCalzone
Vetnametz asked this question in Q&A
Discussion options

You must be logged in to vote

It looks like fromEvent infers the type of event handler argument in a way that does not consider the actual event name, but MqttClient has different handler signatures for each event.
You end up with the arguments for the packetsend event, which is (packet: Packet), when you wanted the arguments for the message event, which is (topic: string, payload: Buffer, packet: IPublishPacket).

I don't see a clean way to fix this, other than overriding the return type:

import { connect, IClientOptions, IPublishPacket, OnMessageCallback, Packet } from "mqtt";
import { fromEvent, type Observable } from "rxjs";
//...
const message$ = fromEvent(powerLinesMQTTClient, "message") as unknown as Observable<P…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@Vetnametz
Comment options

Answer selected by Vetnametz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants