Create CollectionTypes types/interfaces from schema.d.ts

Hello. At my company, we are using Strapi version 4.1.11 in production and we want to upgrade to add Typescript to the project. I’ve been investigating how this could affect our project. One of the most important things is the idea of adding model typings. Currently, I can only see the schemas.d.ts generated but it contains the information of the schema.json file. Attributes of a collection type is there, but there is no specific type to import as a usual DTO or model type.
I’ve seen these utilities from Strapi core:

export type GetAttributesValues<T extends SchemaUID> = {
  // Handle required attributes
  [key in GetAttributesRequiredKeys<T>]-?: GetAttributeValueByKey<T, key>;
} &
  {
    // Handle optional attributes
    [key in GetAttributesOptionalKeys<T>]?: GetAttributeValueByKey<T, key>;
  };

export type GetAttributesRequiredKeys<T extends SchemaUID> = KeysBy<
  GetAttributes<T>,
  { required: true }
>;
export type GetAttributesOptionalKeys<T extends SchemaUID> = keyof Omit<
  GetAttributes<T>,
  GetAttributesRequiredKeys<T>
>;

Are these meant to be used by the user and create types? If not, I cannot see documentation of how to create model types. Is there any plan to add this feature and document it?

Thank you!

1 Like

I don’t know if we should use it, but you can use it like this:

export type User = GetAttributesValues<'plugin::users-permissions.user'>;

Unfortunately, there is no id field in this type.

But this following type can fix this:

export type GetModelType<T extends SchemaUID> = {
  id: number;
} & GetAttributesValues<T>;

export type User = GetModelType<'plugin::users-permissions.user'>;
1 Like

And if you want to have relations:

export type Role = GetModelType<'plugin::users-permissions.role'>;

export type User = GetModelType<'plugin::users-permissions.user'> & {
  role: Role
};

For anyone revisiting this, I found this blog that includes a link to this GitHub gist that makes it easier to work with GetAttributesValues.

It basically does the same thing that marmotz suggested but as it was posted direct on the Strapi blog it just seemed a little more official to me.