简体   繁体   中英

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

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)

If you check the definition of GetItemOutput and the definition of PromiseResult you will see that the promise is returning an object of {Item, ConsumedCapacity, $response} but not the result only. So I think you should use PromiseResult as the type and use the Item attribute as your result.

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);
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM