Feature: Add types in batch get
Discussed in https://github.com/sensedeep/dynamodb-onetable/discussions/337
Originally posted by ShivamJoker May 21, 2022 It would be great to get typescript types in the batchGet response as well.
Right now it returns an array of unknown which is not helpful at all.

I believe I have this one sorted as well. I will open a PR after #369 cause I need those changes
Great! Thank you.
One note on this typing, I've found that batchGet returns a Responses key normally, but when parse: true is set as an option, it returns an array of initialized models. Its a little confusing that setting an option changes the response type so drastically.
Thanks for raising the issue.
That is the case for all APIs. Parse has a big impact.
When you use parse() which is the default for the low level APIs, the returned data is parsed as models.
If not, then it is useful to get the raw responses.
Can you point out where in the doc you found this confusing and we can clarify.
is there any update on above issue?
Sorry, nothing yet. Do you want to try to propose a solution.
@mobsense one solution would be to use Generic type:
batchGet<T = {}>(batch: any, params?: OneParams): Promise<T[]>
so I could use:
const users = await this.table.batchGet<User>(params.batch, params)
Sorry for the slow answer.
But batchGet is typically used to fetch multiple different types. I suppose you could provide a union as the generic type: e.g. User | Account | Item ....
If you just want to fetch users, then find() would be a better choice.
The downside of that change to use a generic type would be that it would break existing TS code would it not?
@mobsense
I have use case sometimes where I need to use batchGet instead of find e.g. to get multiple users by ids.
Passing User | Account | Item is fine because we would have generic function
Sorry for the slow answer.
But batchGet is typically used to fetch multiple different types. I suppose you could provide a union as the generic type: e.g. User | Account | Item ....
If you just want to fetch users, then find() would be a better choice.
The downside of that change to use a generic type would be that it would break existing TS code would it not?
it shouldn't (depends on the ts-config) as generic type would be optional because of default initialisation <T = {}>
batchGet<T = {}>(batch: any, params?: OneParams): Promise<T[]>
use type
const users = await this.table.batchGet<User>(params.batch, params)
or without type
const users = await this.table.batchGet(params.batch, params)
We'll commit that change. If you and other could test and if okay, we'll add to the next point release.
@mobsense yeah I can test it :)