Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

typescript - How to properly add return type to aws-sdk DynamoDb Send command

I am trying to get a generic type back as part of my OutputType when I do a DynamoDB Get using the new v3 aws-sdk.

My function looks like:

public async getItem<T>(data: GetItemCommandInput) {
  const command = new GetItemCommand({
    ...data
  });
  return await this.dbClient.send<GetItemCommand, T>(command);
}

Typescript is not happy with putting the T in the Send call. Looking at the typescript definition, it is pretty messy and I don't understand what they are doing. The first Type Send expects is of type ClientInput, and the second one of ClientOutput. Looking at the definition of ClientOutput my jaw just drops open:

ClientOutput extends MetadataBearer, ResolvedClientConfiguration extends SmithyResolvedConfiguration<HandlerOptions>> implements IClient<ClientInput, ClientOutput, ResolvedClientConfiguration>

....okay, they lost me.

Normally I would expect something like:

 await this.dbClient.send<GetItemCommandInput, T>(command);
or 
return await this.dbClient.send<GetItemCommandInput, GetItemCommandOutput<LegalAccount>>(command);

but GetItemCommandOutput doesn't allow for a generic.

Looking at the source typing I see:

export interface GetItemOutput {
    /**
     * <p>A map of attribute names to <code>AttributeValue</code> objects, as specified
     *             by <code>ProjectionExpression</code>.</p>
     */
    Item?: {
        [key: string]: AttributeValue;
    };

My whole point is that when I get the result of the query, I know that the Item inside the output is of type T. Did AWS just forget this or am I missing something. Docs are totally NOT helpful as expected :(

question from:https://stackoverflow.com/questions/65896644/how-to-properly-add-return-type-to-aws-sdk-dynamodb-send-command

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...