dynamodb-toolbox icon indicating copy to clipboard operation
dynamodb-toolbox copied to clipboard

[v0.0.4 TypeScript] Type inference for `Entity.get` seems to result in `any`

Open mxro opened this issue 2 years ago • 0 comments

Using the current alpha version (0.4.0-alpha.2), it seems that while type inference seems to work for checking the arguments passed to Entity.put, it doesn't work for giving the correct return value for Entity.get.

See in the example below table.spect.ts#L44:

  const table = new Table({
      name: await getTableName(),
      partitionKey: 'pk',
      sortKey: 'sk',
      DocumentClient: new DynamoDB.DocumentClient({ service: await connect() }),
    });

    const e = new Entity({
      name: 'User',
      attributes: {
        pk: { partitionKey: true },
        sk: { hidden: true, sortKey: true },
        name: { type: 'string', required: true },
        emailVerified: { type: 'boolean', required: true },
      },
      table,
    } as const);

    await e.put({
      pk: '[email protected]',
      sk: 'd',
      name: 'Joe',
      emailVerified: true,
    });

    const { Item: user } = await e.get(
      { pk: '[email protected]', sk: 'd' },
      { attributes: ['name', 'pk'] }
    );

    expect(user.name).toEqual('Joe');
    await stopLocalDynamoDB();

The type for the variable user is returned as any.

image

Anything I am doing wrong here?

I tried a few variations of this, including trying to manually overlay the type in get by specifying the entity and key type manually:

    const { Item: user } = await e.get<User, UserKey>(
      { pk: '[email protected]', sk: 'd' },
      { attributes: ['name', 'pk'] }
    );

However, this also results in user having the type of any. This is using TypeScript 4.4.3.

mxro avatar May 14 '22 22:05 mxro