Hi everyone,
I’m using the Directus SDK with TypeScript, and I’m trying to figure out the best way to handle typing for relational collections.
For example, I have two collections: products and variants, where each product has one or more variants.
Here’s a simplified version of my types:
interface Variant = {
id: number;
name: string;
price: number;
};
interface Product = {
id: number;
name: string;
variants: number[] | Variant[];
};
interface Schema = {
products: Product[];
variants: Variant[];
};
I create the client like this:
const directus = createDirectus<Schema>('http://directus.example.com').with(rest());
What’s the best way to handle this from a typing perspective?
What I’d like to do is write two readItems functions for products:
- One that returns products with just the variant IDs
- Another that returns products with full Variant objects populated
I’ve seen a bunch of possible ways to handle this, but I’d really like to know how someone with more experience would approach it