Encountering an Error When Defining Types as an Array of createdEvents in TypeScript
When trying to define the types as an array of createdEvents or string, I am facing the following error:
Type '(parent: User, args: InputShapeFromFields<InputFieldMap<"InputObject" | "Arg">>, context: { currentUser: User; }) => "No events created" | (string | createdEvents[])[]' is not assignable to type 'Resolver<User, InputShapeFromFields<InputFieldMap<"InputObject" | "Arg">>, { currentUser: User; }, createdEvents[] | null | undefined, "No events created" | (string | createdEvents[])[]>'. Type '"No events created" | (string | createdEvents[])[]' is not assignable to type 'MaybePromise<readonly MaybePromise[]> | null | undefined'. Type '"No events created"' is not assignable to type 'MaybePromise<readonly MaybePromise[]> | null | undefined'.
The error seems to indicate that the field I am creating is not compatible with the User type.
I have attempted the following:
- Returning parent.createdEvents alone
- If it's undefined, return null, undefined, and an empty array
However, none of these solutions work. Interestingly, adding "any" as an option for the user createdEvents type definition resolves the issue as shown below:
export class User {
_id: ObjectId;
name: string;
email: string;
password?: string;
availableWeights?: number[];
createdEvents?: createdEvents[] | string;
signedUpEvents?: userSignUp[];
constructor(id: ObjectId, name: string, email: string, password?: string, availableWeights?: number[],
createdEvents?: createdEvents[] | string, signedUpEvents?: userSignUp[]) {
this._id = id;
this.name = name;
this.email = email;
this.password = password;
this.availableWeights = availableWeights;
this.createdEvents = createdEvents;
this.signedUpEvents = signedUpEvents;
}
}
By appending "| any" to the end of the createdEvents field, the error disappears.
Error Encountered in Resolve Function:
createdEvents: t.field({
type: [createdEvents],
resolve: (parent, args, context) => {
if (parent.createdEvents == undefined || parent.createdEvents == null || parent.createdEvents.length == 0) {
return "No events created";
} else {
return parent.createdEvents;
}
}
}),
Below is the Class Definition Used Throughout the Code:
export class createdEvents {
_id: ObjectId;
eventName: string;
eventDate: Date;
eventDescription: string;
cost?: string;
eventLink?: string;
weights?: weightsForUserCreatedEvents[];
constructor(_id: ObjectId, eventName: string, eventDate: Date, eventDescription: string, cost?: string, eventLink?: string, weights?: weightsForUserCreatedEvents[]) {
this._id = _id;
this.eventName = eventName;
this.eventDate = eventDate;
this.eventDescription = eventDescription;
this.cost = cost;
this.eventLink = eventLink;
this.weights = weights;
}
}