How to define a return type for a DynamoDB get with TypeScript?

Viewed 1835

I have:

  let resItem: Schema

  resItem = await dynamoClient.get({
    TableName,
    Key: {
      uuid: request.body.uuid
    }
  }).promise()

but I get:

Type 'PromiseResult<GetItemOutput, AWSError>' is missing the following properties from type 'Schema': uuid, topics, phoneNumber, timezonets(2739)
2 Answers

Here you go. This is known as the lazy way. As there is no way of knowing if the properties in the type ReturnType are actually there at runtime.

But the content is coming from a database call and we expect dynamoDB to work, that's why we don't unit test it.

export const get = async <ReturnType>(params: DynamoDB.DocumentClient.GetItemInput) => {
  try {
    const result = await dynamodb.get(params).promise();
    return {...result, Item: result.Item as ReturnType};
  } catch (error) {
    throw Error(error);
  }
};
Related