TypeScript Error: Property 'pages' Does Not Exist on Story Type
I’m working with the getGeneratedStories function, and I’ve included the pages in the query like this:
export const getGeneratedStories: GetGeneratedStories<void, Story[]> = async (
_args,
context,
) => {
if (!context.user) {
throw new HttpError(401);
}
const stories = await context.entities.Story.findMany({
where: {
userId: context.user.id,
deletedAt: null,
},
include: {
pages: true,
},
});
return stories;
};
However, when I try to access story.pages in my component, I get a TypeScript error stating that the property 'pages' does not exist on type. The code works fine at runtime, but I can't seem to get TypeScript to recognize the pages property.
Here’s how I’m trying to access it:
{stories.map((story) => {
const isDisabled =
story.status === "pending" || story.status === "generating";
// TypeScript error here
const pageCount = story?.pages?.length || 0;
return (
<></>
);
})}
Could anyone help me understand why this TypeScript error occurs and how I can fix it? Thanks in advance!
The Story model already includes the pages field in the Prisma schema2 Replies
Hi @omerbargon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Hmm, I don't know enough to give you a confident answer yet. The knowledge sources provided do not contain information about the
getGeneratedStories
function or the specific TypeScript error you're encountering.
However, generally, TypeScript errors like this occur when the TypeScript compiler cannot infer the correct type for an object or variable. In your case, it seems like TypeScript is not aware that the Story
type includes a pages
property.
One possible solution could be to define an interface for the Story
type that includes the pages
property, and use that interface in your getGeneratedStories
function and wherever you're using the Story
type. This would give TypeScript the information it needs to correctly type-check your code.
If you're still having trouble, I would recommend checking the Wasp documentation (https://wasp-lang.dev/docs) or asking for help in the Wasp Discord community (https://discord.com/channels/686873244791210014/1204021576979324949).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @omerbargon, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?Check this bit in the docs: https://wasp-lang.dev/docs/data-model/operations/actions#implementing-actions-in-node
Espacially the green box that talks about the
satisfies
in Typescript.
You are defining the action return type as being a list of Story
items. But - it's not just stories, it includes also pages
so that's an extra field that the resulting objects include. When we define the return type explicitly like you did, we are saying to Typescript - I know what I'm doing, don't try to automatically infer the return type
Using the technique I mentioned above, we can let Typesctipt infer this for itself that the return type is a list of items of type Story + some extra field and the types will be correct.Actions | Wasp
We'll explain what Actions are and how to use them. If you're looking for a detailed API specification, skip ahead to the API Reference.