Hi!
i recently started to use strapi and am struggleing miserably with typings.
My main question is: How is typescript even supposed to be used in strapi?
E.g. when I generate my types I get something like:
export interface ApiSubscriptionSubscription extends Schema.CollectionType {
collectionName: 'subscriptions';
// ... the only thing that interests me:
attributes: {
title: Attribute.String & Attribute.Required;
included: Attribute.Component<'simple-types.bulletpoint', true>;
stripeId: Attribute.String;
// ...
}
}
declare module '@strapi/strapi' {
export module Shared {
export interface ContentTypes {
// ...
'api::subscription.subscription': ApiSubscriptionSubscription;
}
}
}
Than, when I try to use it in my code, Should I import it like this?
import type { Shared } from "@strapi/strapi";
interface Subscription = Shared.ContentTypes["api::subscription.subscription"]["attributes"]
const subs: Subscription[] = await strapi
.query("api::subscription.subscription")
.findMany(...);
And how do I deal with types like relations?
E.g. I have to following generated type:
export interface ApiCustomerCustomer extends Schema.CollectionType {
// ...
attributes: {
// ...
subscription: Attribute.Relation<
'api::customer.customer',
'oneToOne',
'api::subscription.subscription'
>;
// ...
};
}
How do I use this in code?
// previous code:
interface Subscription = Shared.ContentTypes["api::subscription.subscription"]["attributes"]
// ...
if (customer) {
return {
// ...
subscription: {
id:
(customer.subscription as any as Subscription)?.stripeId || null,
active: isSubscriptionActive(
customer.subscriptionStatus as unknown as StripeSubscriptionStatus,
customer.subscriptionPeriod
),
period: customer.subscriptionPeriod,
},
};
}
Without casting subscription to any and than to Subscription, I get the error:
Property ‘stripeId’ does not exist on type ‘Relation<“api::customer.customer”, “oneToOne”, “api::subscription.subscription”>’.
Other example:
included: Attribute.Component<"simple-types.bulletpoint", true>;
When I want to map over the attribute, e.g.:
included: sub.included.map((inc) => inc.label),
I get Property 'map' does not exist on type 'Component<"simple-types.bulletpoint", true>'.
I am really struggling with this, and would be very happy if someone could help me out here, especially since this all happened after my upgrade from 4.9 to 4.11.3.
System Information
- Strapi Version: 4.11.3