typesafe-dynamodb: Incorrect type inference from GetItem command (v3)
The type of Item appears to be undefined. I expected it to be IAccount

Here is the code
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
const client1 = new DynamoDBClient({});
export const docClient = DynamoDBDocumentClient.from(
client1
) as TypeSafeDocumentClientV3<IAccount, 'pk', 'sk'>;
const res1 = await docClient.send(
new TypedGetItemCommand({
TableName: 'Account',
Key: {
pk: 'ACCOUNT#foo',
sk: 'ACCOUNT#foo',
},
})
);
res1.Item;
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (11 by maintainers)
I don’t think we can workaround this issue. It is not an expected use-case to have hard-coded suffixes in the keys that aren’t properly represented in the type (
IAccount
). It will only work if the types are parameterized.You can see the general problem here -
ACCOUNT#${string}
is not assignable toACCOUNT#abc
and therefore narrows tonever
. Maybe TS could be smarter here and realize thatACCOUNT#${string}
should be narrowed to the literal stringACCOUNT#abc
. I will cut an issue with the TypeScript team and see what they think.Got it, now I can repro. Will dig in.
The problem is in the generics. Try with this type: