I’m currently working with Strapi v5 and TypeScript, and I’m trying to understand how to access the types provided by Strapi. For example, if I want to create a new user, how can I get the base object to validate my data in TypeScript?
Is there any recommended way to access the built-in types for the models or entities I define in Strapi so I can use them for validation or type checking?
Any help or guidance would be greatly appreciated! Thanks in advance!
This topic has been created from a Discord post (1289124291891298324) to give it more visibility.
It will be on Read-Only mode here. Join the conversation on Discord
There are a few scripts that allows you to copy the types to your frontend but they are for V$. I ended up doing the types manually on the frontend with the V5
There is a brilliant blog post that is focused on v4 (Improve Your Frontend Experience With Strapi Types And TypeScript), and we used it just a month ago to set up a working type system. That said, the types it uses under the hood no longer work in v5. I am trying to recreate the same setup in v5, and I’m willing to write a blog post about the outcomes. <@960590891200970812> sorry for pinging you here, but is there any chance you could help me out with that?
I was able to make most of the types work by using Modules.Documents.GetValues from @strapi/strapi, but now all fields that are marked as required are considered optional by the compiler, i.e.
pages/[...uid].page.tsx:167:11 - error TS2322: Type 'string | null | undefined' is not assignable to type 'string | undefined'.
167 description={metaDescription}
I’ve seen your message in the TS thread on git, I think your first approach was correct, the problem with required props is that at you can only be sure that values is not null or undefined only by validating it…
Thanks for the suggestion! I understand the need for validation, but I can trust the content type “required” validation at least in my case (although this won’t be the case when previewing drafts). I looked into Modify and it seems it won’t be generic enough and would have to be customized each time.
I managed to get the desired behavior with this:
export type GetValues<TSchemaUID extends UID.Schema> = Modules.Documents.Params.Attribute.GetValues<TSchemaUID>;
It works perfectly, but behaves weirdly in a couple cases: relation fields are typed incorrectly, and uploads show up as any.