I need to wrap functions and pass in the wrapped-functions’ arguments as parameters in the wrapping-functions. Rather than create interfaces for arguments for each instance of wrapped functions, I was trying to create a type that infers this using generic types, however I end up with a tuple type that cannot be spread into the wrapped-function.
From what I have right now, is there a way I can achieve this in typescript?
I’ve tried the following
/**
* Model instances
* @description These are the instances of the models that will be used to interact with the database.
*/
const models = {
session: Session(prisma.session),
user: User(prisma.user)
}
/**
* A type for the arguments of resolver functions
*/
type ModelArguments<
M extends keyof typeof models,
T extends keyof {
[P in keyof typeof models[M] as (
typeof models[M][P] extends Function ? P : never
)] : never
}
> = Parameters<typeof models[M][T]>
/**
* GraphQL resolvers
* @description This is the object that will contain the resolvers for the queries defined in the schema.
*/
const resolvers = {
Query: {
// session: async (p, args: ModelArguments<"session", "get">) => await models.session.get(),
// user_sessions: async (p, args: ModelArguments<"session", "getByUser">) => await models.session.getByUser(),
user: async (p, args: ModelArguments<"user", "get">) => {
console.log(args); // {"userID": <user-id>}
return await models.user.get(args.userID) // TS2339: Property userID does not exist on type [userID?: string]
}
},
};
However on the return line for resolvers.Query.user
I get the Typescript error TS2339: Property userID does not exist on type [userID?: string]
and I’m not sure how this is possible
Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.