首页 > 解决方案 > 定义队列将在 Nodejs 中接受的消息类型

问题描述

使用的技术堆栈是Typescript、Express、Nodejs、Mongoose 和 Mongodb

Nodejs 的Bull库用于管理Queues

我们可以限制队列接受的消息类型吗?

const data1 = {name: "Alpha", Age: 30};
const data1 = {name: "Beta", Age: 40};
const data3 = {color: "Red", taste: "sweet"}; // queue should give error on some other message structire being passed

queue.add(data1);
queue.add(data2);
queue.add(data3); // error should come here
queue.add 

在泛型的帮助下,使用 Java 等语言很容易做到这一点。

在打字稿中实现相同的任何方法?

标签: node.jstypescriptgenericsqueue

解决方案


您应该能够在实例化队列期间提及您想要的特定类型 -

interface QueueData {
  name: string;
  Age: number;
}

const myQueue = new Queue<QueueData>('name', options);

推荐阅读