I'm trying to consolidate my DynamoDB functions that query and put items to my DB, however the typescript is causing me issues.
Original
export const getResultFromDb = async (): Promise<QueryCommandOutput> => {
const command = new QueryCommand({
...
});
try {
return await client.send(command);
} catch (err) {
// log out
}
};
export const addItemToDb = async (data: any): Promise<PutItemCommandOutput> => {
const command = new PutItemCommand({
...
});
try {
return await client.send(command);
} catch (err) {
// Log out
}
}
I have attempted to move the main try/catch
logic into its own function
const queryDb = async <T>(command: any): T => {
try {
return await client.send(command);
} catch (err) {
// Log out
}
}
with the idea of specifying my output types like
export const getResultFromDb = async (): Promise<QueryCommandOutput> => {
const command = new QueryCommand({
...
});
return await queryDb<QueryCommandOutput>(command);
};
export const addItemToDb = async (data: any): Promise<PutItemCommandOutput> => {
const command = new PutItemCommand({
...
});
return await queryDb<PutItemCommandOutput>(command);
}
Asking AI about the types for the command
arg, it suggested that I should use @aws-sdk/smithy-client
's "Command" like
const queryDb = async <T>(command: Command<any, T, any>): T => {
but the IDE didn't like this very much:
TS2345: Argument of type Command<any, T, any, any, any> is not assignable to parameter of type
Command<ServiceInputTypes, any, ServiceOutputTypes, ServiceOutputTypes, SmithyResolvedConfiguration<HttpHandlerOptions>>
Types of property middlewareStack are incompatible.
Property identifyOnResolve is missing in type MiddlewareStack<any, T> but required in type MiddlewareStack<any, ServiceOutputTypes>
middleware. d. ts(432, 5): identifyOnResolve is declared here
I'm trying to consolidate my DynamoDB functions that query and put items to my DB, however the typescript is causing me issues.
Original
export const getResultFromDb = async (): Promise<QueryCommandOutput> => {
const command = new QueryCommand({
...
});
try {
return await client.send(command);
} catch (err) {
// log out
}
};
export const addItemToDb = async (data: any): Promise<PutItemCommandOutput> => {
const command = new PutItemCommand({
...
});
try {
return await client.send(command);
} catch (err) {
// Log out
}
}
I have attempted to move the main try/catch
logic into its own function
const queryDb = async <T>(command: any): T => {
try {
return await client.send(command);
} catch (err) {
// Log out
}
}
with the idea of specifying my output types like
export const getResultFromDb = async (): Promise<QueryCommandOutput> => {
const command = new QueryCommand({
...
});
return await queryDb<QueryCommandOutput>(command);
};
export const addItemToDb = async (data: any): Promise<PutItemCommandOutput> => {
const command = new PutItemCommand({
...
});
return await queryDb<PutItemCommandOutput>(command);
}
Asking AI about the types for the command
arg, it suggested that I should use @aws-sdk/smithy-client
's "Command" like
const queryDb = async <T>(command: Command<any, T, any>): T => {
but the IDE didn't like this very much:
TS2345: Argument of type Command<any, T, any, any, any> is not assignable to parameter of type
Command<ServiceInputTypes, any, ServiceOutputTypes, ServiceOutputTypes, SmithyResolvedConfiguration<HttpHandlerOptions>>
Types of property middlewareStack are incompatible.
Property identifyOnResolve is missing in type MiddlewareStack<any, T> but required in type MiddlewareStack<any, ServiceOutputTypes>
middleware. d. ts(432, 5): identifyOnResolve is declared here
Share
Improve this question
asked Mar 25 at 14:49
physicsboyphysicsboy
6,37822 gold badges77 silver badges139 bronze badges
1
- Great to see TypeScript giving C++ compiler teams a little competition in the obfuscation department. Also, I think that @aws-sdk/smithy-client is deprecated and has moved to @smithy/smithy-client. – jarmod Commented Mar 25 at 15:26
1 Answer
Reset to default 0You can reply on type of client.send
function:
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
const queryDB: DynamoDBClient['send'] = async (
command: Parameters<DynamoDBClient['send']>[0],
) => {
try {
return await client.send(command);
} catch (e) {
console.error(e);
}
};
Now, you can use queryDB
as the .send
function:
const command = new PutItemCommand({
...
});
const output = await queryDB(command);
// ^? PutItemCommandOutput