Skip to content

Current Typescript definitions are difficult to use to read userProperties #1248

Closed
@bkp7

Description

@bkp7

Using TypeScript, reading user properties requires something like this:

client.on('message', function(topic, message, packet) {
  const pckt: IPublishPacket = Object.assign(packet);
  if (pckt.properties && pckt.properties.userProperties) {
    const userProperties: {[index: string]: string} = Object.assign(pckt.properties.userProperties);
    console.log(userProperties['test']);
  }
});

It would be better if the userProperties (in mqtt-packet) were changed from being {object} to being {[index: string]: string}.

It would also be better if the on message callback required an IPublishPacket rather than the more generic Packet.

Changing both these would allow type assignments to be removed and the TypeScript code could be changed to:

client.on('message', function(topic, message, packet) {
  if (packet.properties && packet.properties.userProperties) {
    console.log(packet.properties.userProperties['test']);
  }
});

Linked to mqttjs/mqtt-packet#102

AB#9320335

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions