typesafe-dynamodb: Incorrect type inference from GetItem command (v3)

The type of Item appears to be undefined. I expected it to be IAccount

Screen Shot 2022-06-19 at 5 40 19 PM

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)

Most upvoted comments

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 to ACCOUNT#abc and therefore narrows to never. Maybe TS could be smarter here and realize that ACCOUNT#${string} should be narrowed to the literal string ACCOUNT#abc. I will cut an issue with the TypeScript team and see what they think.

image

Got it, now I can repro. Will dig in.

The problem is in the generics. Try with this type:

export interface IAccount<
  id extends string = string,
  key extends string = string
> {
  pk: `ACCOUNT${id}`;
  sk: `ACCOUNT${key}`;
}