Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

get,put,update method type inference incorrect when extending Entity #661

Open
ojongerius opened this issue Jan 29, 2024 · 3 comments
Open
Assignees

Comments

@ojongerius
Copy link
Contributor

ojongerius commented Jan 29, 2024

馃憢 I have an issue with extending entities. When extending the Entity class I see the following unexpected behaviour:

The inferred value for the get, put and update methods return {Get,Put,Update>InputCommand instead of {Get,Put,Update}OutputCommand. When I look your code for the get method at https://github.com/jeremydaly/dynamodb-toolbox/blob/main/src/classes/Entity/Entity.ts#L289 I see that return value is defined as:

Promise<If<B.Not<ShouldExecute<Execute, AutoExecute>>,
    GetCommandInput,
    If<B.Not<ShouldParse<Parse, AutoParse>>,
      GetCommandOutput,
      Compute<O.Update<GetCommandOutput,
        'Item',
        FirstDefined<[MethodItemOverlay, Compute<O.Pick<Item, ResponseAttributes>>]>>>>>

The behaviour I see during type inference is that either Execute or AutoExecute is set to false. The generated JS code behaves as expected AFAICT. This behaviour changes calling those methods with { execute: true }.

As far as I can tell this was introduced by #302 and this behaviour can be changed by changing (https://github.com/jeremydaly/dynamodb-toolbox/blob/main/src/classes/Entity/Entity.ts#L74) from:

  AutoExecute extends boolean = string extends Name ? boolean : true,

to

  AutoExecute extends boolean = true,

But obviously a broader fix would be necessary.

Reproduce:

Config:

 const config = {
    name: 'TestEnt',
    attributes: {
      pk: { partitionKey: true },
      test: { map: 'sk' },
    },
    autoParse: false,
    table: new Table({
      name: 'base-table',
      partitionKey: 'pk',
      DocumentClient,
    }),
  } as const
 
  class TestEntityExtended extends Entity {
    constructor() {
      super(config)
    }
  }

Using this entity inferred return value looks fine, tested with:

  const baseEntity = new Entity(config);
    const baseEntity = new Entity(config);
    const res = baseEntity.get({ pk: 'foo' }) // Promise<GetOutPutCommand>

    type TestExtends = Equals<typeof res, Promise<GetCommandOutput>>
    const testExtends: TestExtends = 1 // Expected output
    expect(testExtends).toBe(1)

Using TestEntityExtended, we get Promise<GetCommandInput>

    const testEntity = new TestEntityExtended()
    const res = testEntity.get({ pk: 'foo' })
    type TestExtends = Equals<typeof res, Promise<GetCommandOutput>>  // FAIL (Promise<GetCommandInput> instead)
    const testExtends: TestExtends = 1 // 
    expect(testExtends).toBe(1)

Working around by passing { execute: true } we get Promise<GetOutPutCommand> and test passes

    const TestEntity = new TestEntityExtended()
    const bar = TestEntity.get({ pk: 'foo' }, { execute: true })
    type TestExtends = Equals<typeof bar, Promise<GetCommandOutput>>
    const testExtends: TestExtends = 1
    expect(testExtends).toBe(1)
  })
@ojongerius
Copy link
Contributor Author

Pushed demo in #662

@ojongerius ojongerius changed the title get,put,update method type inferrence incorrect when extending Entity get,put,update method type inference incorrect when extending Entity Jan 29, 2024
@ojongerius
Copy link
Contributor Author

Similar but not identical: #367

@ojongerius
Copy link
Contributor Author

Related: #683

@naorpeled naorpeled self-assigned this Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants