# What is the best TypeScript Approach for Relational Collections using SDK?

**URL:** <https://community.directus.com/t/what-is-the-best-typescript-approach-for-relational-collections-using-sdk/315>\
**Category:** Help\
**Created:** [May 14, 2025, 1:59pm UTC](https://community.directus.com/t/what-is-the-best-typescript-approach-for-relational-collections-using-sdk/315 "2025-05-14T13:59:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fce192](https://avatars.discourse-cdn.com/v4/letter/f/ee7513/32.png) [@fce192](https://community.directus.com/u/fce192)\
**Post date:** [May 14, 2025, 1:59pm UTC](https://community.directus.com/t/what-is-the-best-typescript-approach-for-relational-collections-using-sdk/315/1 "2025-05-14T13:59:17Z")

</div>

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:

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![bryantgillespie](https://sea2.discourse-cdn.com/flex002/user_avatar/community.directus.com/bryantgillespie/32/33_2.png) [@bryantgillespie](https://community.directus.com/u/bryantgillespie)\
**Post date:** [May 15, 2025, 2:11pm UTC](https://community.directus.com/t/what-is-the-best-typescript-approach-for-relational-collections-using-sdk/315/2 "2025-05-15T14:11:47Z")

</div>

Great question. And welcome to the community 👋

Here’s a standard format that we recommend in the docs.

> **[Sdk | Directus Docs](https://directus.io/docs/guides/connect/sdk#create-a-client)**
>
> Explore our resources and powerful data engine to build your projects confidently.

```ts
import { createDirectus, rest } from '@directus/sdk';

interface Post {
  id: number;
  title: string;
  content: string;
}

interface Schema {
  posts: Post[];
}

const directus = createDirectus<Schema>('http://directus.example.com').with(rest());

```

We currently don’t have an **official** types generator but no worries.

There are several community extensions that you can add to your instance through the marketplace or npm.

Or you can use this package that I created for our starter templates to automate the type generation process.

> **[GitHub - bryantgillespie/directus-sdk-typegen: CLI and library to generate TypeScript types from...](https://github.com/bryantgillespie/directus-sdk-typegen)**
>
> CLI and library to generate TypeScript types from Directus collections for use with Directus SDK.
