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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type Inference Issue with Entity.get()
When Used Inside a Class Constructor
#683
Comments
the same happens with Entity.update - for some reason typescript thinks, that the UpdateCommandInput is returned from .update() |
Getting same error with
|
Hey. So this is really a typescript issue/understanding. When you use
I have missed out a lot of the generics here as there are many and the above is just an example. The key one in your case is the last one This will fix your problem but requires all generics to be properly defined to prevent other problems :) |
it didn't fix my issue unfortunately, but I have a bit more complex scenario const jobEntity = new Entity<typeof jobEntityName, Job, CompositeKey, typeof appTable, true>({
name: jobEntityName,
table: appTable,
.... I use repository pattern: class JobRepository extends DynamoDbRepository<typeof jobEntity> {
constructor() {
super(appTable, jobEntity);
}
... export abstract class DynamoDbRepository<E extends Entity> {
constructor(
private readonly table: Table<any, any, any>,
private readonly entity: E,
) {}
get<O extends GetOptions<E>>(primaryKey: { pk: string; sk: string }, options: O = {} as O) {
return this.entity.get(primaryKey, options);
}
... and get operation still throws the error: // TS2339: Property 'Item' does not exist on type 'GetCommandInput'.
const { Item } = await this.jobRepository.get({ pk: '', sk: '' }); |
@naorpeled it is not clear what you suggest to do, can you please provide a working example? |
Hey @nevolgograd, |
Hey @nevolgograd, Need to give this more thought but not 100% sure how to resolve this within v0 atm. |
@naorpeled thanks for investigating, it felt like something confusing for a reason. p.s. huge thanks for this tool, I love it so much! |
@naorpeled |
Describe the bug
When using the
Entity.get()
method from thedynamodb-toolbox
package within a TypeScript class, type inference works as expected if the Entity and Table instances are created outside of a class constructor. However, moving the instantiation inside the class constructor causes TypeScript to fail to infer the correct type for theItem
from the DynamoDB response, resulting in a type error.To Reproduce
Steps to reproduce the behavior:
Table
and anEntity
instance inside a TypeScript class constructor.Entity.get()
method to fetch an item from DynamoDB.Property ‘Item’ does not exist on type ‘GetCommandInput’. ts(2339).
Expected behavior
TypeScript should correctly infer the type of the
Item
returned by theEntity.get()
method(and other methods), similar to when theTable
andEntity
instances are created outside the class constructor.Code Snippets
Working scenario (Outside Constructor):
Non-working scenario (Inside Constructor):
The text was updated successfully, but these errors were encountered: