How to correctly type the batch?

async queue(batch: MessageBatch<{ bucket: string }>, env): Promise<void> {
// A queue consumer can make requests to other endpoints on the Internet,
// write to R2 object storage, query a D1 Database, and much more.
for (let message of batch.messages) {
const { body } = message;

const bucket = body.bucket;

// Process each message (we'll just log these)
console.log('Received queue', batch.queue);
console.log('Received messages', batch.messages);

console.log(`message ${message.id} processed: ${JSON.stringify(message.body)}`);
}
},
async queue(batch: MessageBatch<{ bucket: string }>, env): Promise<void> {
// A queue consumer can make requests to other endpoints on the Internet,
// write to R2 object storage, query a D1 Database, and much more.
for (let message of batch.messages) {
const { body } = message;

const bucket = body.bucket;

// Process each message (we'll just log these)
console.log('Received queue', batch.queue);
console.log('Received messages', batch.messages);

console.log(`message ${message.id} processed: ${JSON.stringify(message.body)}`);
}
},
For whatever reason, batch is typed as MessageBatch<Error>. Do I have to configure this type myself, it would be nice if Workers had defined types for different event notifications. I only expect queue messages that come from created object notifications out of R2, so maybe this type should exist for ease of use. I also can't find where in the docs this topic is explored.
1 Reply
Json 🧈
Json 🧈OP•4w ago
I can explicitly specify the type by modifying the use of satisfies, but this is not ideal since they type isn't generate by wrangler.
satisfies ExportedHandler<Env,
{
account: string;
bucket: string;
eventTime: string;
action: string;
object: {
key: string;
eTag: string;
size: number;
};
}
>
satisfies ExportedHandler<Env,
{
account: string;
bucket: string;
eventTime: string;
action: string;
object: {
key: string;
eTag: string;
size: number;
};
}
>

Did you find this page helpful?