Looking to develop a function named batchUsers
, requiring a parameter of type readonly string
in order to create a DataLoader. However, when calling the User.findBy
function within my batchUsers
function, it's causing issues due to conflicting parameter types.
type BatchUser = (ids: readonly string[]) => Promise<User[]>;
const batchUsers:BatchUser = async (ids) => {
const users = await User.findBy({ id: In(ids) });
// rearranging the users to match the ids
const userMap: { [key: string]: User } = {};
users.forEach((u) => {
userMap[u.id] = u;
})
return ids.map((id) => userMap[id]);
}
export const userLoader = () => new DataLoader<string, User>(batchUsers)
The error message received is:
The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'string[]'
concerning the ids used in the findBy function.
If I try changing the type definition as follows:
type BatchUser = (ids: string[]) => Promise<User[]>;
Another error arises:
Argument of type 'BatchUser' is not assignable to parameter of type 'BatchLoadFn<string, User>'.
Types of parameters 'ids' and 'keys' are incompatible.
The type 'readonly string[]' is 'readonly' and cannot be assigned to the mutable type 'string[]'
This occurs on the final line where the new Dataloader instance is created.
Seeking suggestions on how to resolve this issue.