【问题标题】:How to define a return type for a DynamoDB get with TypeScript?如何使用 TypeScript 为 DynamoDB 定义返回类型?
【发布时间】:2021-10-10 01:49:52
【问题描述】:

我有:

  let resItem: Schema

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

但我明白了:

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

【问题讨论】:

    标签: node.js typescript amazon-dynamodb


    【解决方案1】:

    如果您检查the definition of GetItemOutputthe definition of PromiseResult,您将看到promise 返回{Item, ConsumedCapacity, $response} 的对象,但不仅仅是结果。所以我认为你应该使用PromiseResult 作为类型并使用Item 属性作为你的结果。

    【讨论】:

      【解决方案2】:

      给你。这被称为惰性方式。因为无法知道 ReturnType 类型中的属性在运行时是否真的存在。

      但内容来自数据库调用,我们希望 dynamoDB 能够正常工作,这就是我们不对它进行单元测试的原因。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-05
        • 1970-01-01
        • 2020-09-14
        • 2020-01-13
        • 1970-01-01
        • 1970-01-01
        • 2012-10-19
        相关资源
        最近更新 更多