Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot make Directus SDK types match. #11913

Closed
3 tasks done
Vlad-Rotaru opened this issue Mar 2, 2022 · 5 comments · Fixed by #13214
Closed
3 tasks done

Cannot make Directus SDK types match. #11913

Vlad-Rotaru opened this issue Mar 2, 2022 · 5 comments · Fixed by #13214

Comments

@Vlad-Rotaru
Copy link

Vlad-Rotaru commented Mar 2, 2022

Preflight Checklist

Describe the Bug

I cannot request for a resource and it's translations because the Type for the deep property of the query expects and instance ofthe resource and not of it's translation.

To Reproduce

I am trying to set up the Directus SDK in a project and i have resources that have translations. I have currently created them as follows:

export type MarketingTile = {
  id?: number;
  pageId?: number;
  status?: string;
  translations: number[] | MarketingTileTranslation[];
};

export type MarketingTileTranslation = {
  languagesCode?: string;
  headline?: string;
  text?: string;
  image?: string;
};

When trying to make a request for marketing tiles including translations i write:

this.directus.items('marketingTiles').readByQuery({
  fields: ['', 'translations.'],
  deep: { translations: { _filter: { languagesCode: { _eq: 'en-US' } } },
});

When i try this i get a type error that i cannot assign that object to the deep parameter because apparently deep expects an instance of MarketingTile and it cannot find the languagesCode property to filter by it. Am i defining the types wrong? How should i go about this?

The code works, but i am using an aggresive linter and cannot get passed this linter error.

Errors Shown

No response

What version of Directus are you using?

9.5.2

What version of Node.js are you using?

14.16.0

What database are you using?

Directus CMS

What browser are you using?

Chrome

What operating system are you using?

Windows

How are you deploying Directus?

running locally

@wfoxall
Copy link
Contributor

wfoxall commented May 3, 2022

I think I've just hit this issue today (using the SDK from Directus 9.9.1).

I think that tweaking the deep type definition in QueryOne<T> as follows solves part of my problem:

Before

export type QueryOne<T> = {
	fields?: keyof T | (keyof T)[] | '*' | '*.*' | '*.*.*' | string | string[];
	search?: string;
	deep?: Record<string, DeepQueryMany<T>>;
	export?: 'json' | 'csv' | 'xml';
	filter?: Filter<T>;
};

After

export type QueryOne<T> = {
	fields?: keyof T | (keyof T)[] | '*' | '*.*' | '*.*.*' | string | string[];
	search?: string;
	deep?: {[K in keyof T]?: DeepQueryMany<T[K]>};
	export?: 'json' | 'csv' | 'xml';
	filter?: Filter<T>;
};

But like you, I am defining my o2m relations as (ID|T)[] and I noticed that the filter, deep and the sort properties can't infer their type as soon as arrays are involved (such as in o2m aliases).

So taking this a step further, would it make sense for the filter, deep and sort properties to resolve any (ID|T)[] to T?
I had a go at implementing that strategy and Typescript seemed happy with all the things I'd expect it to let me do.

Happy to submit a PR if it's useful.

@rijkvanzanten
Copy link
Member

@wfoxall Any PR to improve the typescript setup of the SDK is more than welcome 👍🏻 🚀

@ksoltanidev
Copy link

ksoltanidev commented May 10, 2022

I think I've just hit this issue today (using the SDK from Directus 9.9.1).

I think that tweaking the deep type definition in QueryOne<T> as follows solves part of my problem:

Before

export type QueryOne<T> = {
	fields?: keyof T | (keyof T)[] | '*' | '*.*' | '*.*.*' | string | string[];
	search?: string;
	deep?: Record<string, DeepQueryMany<T>>;
	export?: 'json' | 'csv' | 'xml';
	filter?: Filter<T>;
};

After

export type QueryOne<T> = {
	fields?: keyof T | (keyof T)[] | '*' | '*.*' | '*.*.*' | string | string[];
	search?: string;
	deep?: {[K in keyof T]?: DeepQueryMany<T[K]>};
	export?: 'json' | 'csv' | 'xml';
	filter?: Filter<T>;
};

But like you, I am defining my o2m relations as (ID|T)[] and I noticed that the filter, deep and the sort properties can't infer their type as soon as arrays are involved (such as in o2m aliases).

So taking this a step further, would it make sense for the filter, deep and sort properties to resolve any (ID|T)[] to T? I had a go at implementing that strategy and Typescript seemed happy with all the things I'd expect it to let me do.

Happy to submit a PR if it's useful.

I did run into the same issue with filter types. The same fix seems to work.
filter?: {[K in keyof T]: Filter<T[K]>}; instead of filter?: Filter<T>;

@MarvinXu
Copy link

I'm still having this issue on SDK 12.0.1. It says:

Type '{ translations: { _filter: { languages_code: { _eq: any; }; }; }; }' is not assignable to type 'never'.

@br41nslug
Copy link
Member

I'm still having this issue on SDK 12.0.1. It says:

Type '{ translations: { _filter: { languages_code: { _eq: any; }; }; }; }' is not assignable to type 'never'.

This issue is for a previous version of the SDK before it was rewritten so this cannot be applicable to your problem. Please do look for more recent issues or create one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants