How to get to the $response property in the AWS SQS SendMessageResult with typescript?

396 views Asked by At

I'm sending a message to an SQS queue as follows in typescript:

const queueName = 'some_queue_name';
const foo = 'foo';
const bar = 'bar';

const sqs = new AWS.SQS();
const SendMessageRequest: AWS.SQS.SendMessageRequest = {
  MessageBody: JSON.stringify({ foo, bar }),
  QueueUrl: queueName,
};

try {
  const request = sqs.sendMessage(SendMessageRequest, function (err, data) {
    if (err) {
      console.log('Error', err);
    } 
  });

  const smgResponse: SendMessageResult = await request.promise();

} catch (err) {
  console.log(err);
  expect(err).toBeUndefined();
}

At runtime, the SendMessageRequest shows a $response property but this isn't visible in the object definition. What is the best way to access this property at runtime?

1

There are 1 answers

0
Lauren Yim On

Use a type assertion:

const smgResponse = await request.promise() as SendMessageResult & {$response: unknown}

Replace unknown with whatever type $response is at runtime.

If you are doing a lot of assertions you could create a new type for this:

interface SendMessageResultWithResponse extends {
  $response: unknown
}

// alternatively type SendMessageResultWithResponse = SendMessageResult & {$response: unknown}

const smgResponse = await request.promise() as SendMessageResultWithResponse