Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何向aws sdk DynamoDb Send命令正确添加返回类型_Typescript_Amazon Dynamodb_Aws Sdk - Fatal编程技术网

Typescript 如何向aws sdk DynamoDb Send命令正确添加返回类型

Typescript 如何向aws sdk DynamoDb Send命令正确添加返回类型,typescript,amazon-dynamodb,aws-sdk,Typescript,Amazon Dynamodb,Aws Sdk,当我使用新的v3 aws sdk执行DynamoDB get时,我试图将泛型类型作为输出类型的一部分返回 我的函数如下所示: public async getItem<T>(data: GetItemCommandInput) { const command = new GetItemCommand({ ...data }); return await this.dbClient.send<GetItemCommand, T>(command); }

当我使用新的v3 aws sdk执行DynamoDB get时,我试图将泛型类型作为输出类型的一部分返回

我的函数如下所示:

public async getItem<T>(data: GetItemCommandInput) {
  const command = new GetItemCommand({
    ...data
  });
  return await this.dbClient.send<GetItemCommand, T>(command);
}
好吧,他们把我弄丢了

通常情况下,我会期望:

 await this.dbClient.send<GetItemCommandInput, T>(command);
or 
return await this.dbClient.send<GetItemCommandInput, GetItemCommandOutput<LegalAccount>>(command);
我的全部观点是,当我得到查询结果时,我知道输出中的项是T类型的。
你是忘了还是我遗漏了什么。文档完全没有预期的帮助:(

我不确定这是否能帮助您,我遇到了一个与您类似的问题,我最终发现了

使用此客户端,我可以执行与旧的
aws sdk
类似的查询

您必须像这样设置DynamoDB客户端:

import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
const dynamoClient = new DynamoDBClient();
const ddbDocClient = DynamoDBDocumentClient.from(dynamoClient);
import { GetCommand, GetCommandInput } from "@aws-sdk/lib-dynamodb";
// ...
public async getItem<T>(data: GetCommandInput) {
  const command = new GetCommand({
    ...data
  });
  return await this.ddbDocClient.send(command) as Omit<GetCommandOutput, "Item"> & { Item: T };
}
你的函数可以变成这样:

import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
const dynamoClient = new DynamoDBClient();
const ddbDocClient = DynamoDBDocumentClient.from(dynamoClient);
import { GetCommand, GetCommandInput } from "@aws-sdk/lib-dynamodb";
// ...
public async getItem<T>(data: GetCommandInput) {
  const command = new GetCommand({
    ...data
  });
  return await this.ddbDocClient.send(command) as Omit<GetCommandOutput, "Item"> & { Item: T };
}
从“@aws sdk/lib dynamodb”导入{GetCommand,GetCommandInput}”;
// ...
公共异步getItem(数据:GetCommandInput){
const命令=新的GetCommand({
…数据
});
return wait this.ddbDocClient.send(command)as Omit&{Item:T};
}
如文件所述:

文档客户端通过抽象出属性值的概念,简化了AmazonDynamodb中项目的处理

然后,您可以推断出该类型,并期望它正常工作