-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Update generated Typescript schema for Directus SDK v11 #31
Comments
I found another issue: Optional fields should be be typed When using the current version, optional fields become required and vice-versa. |
Are you talking about the
I would argue that is pretty much up to personal preference. Or are there any objective reasons it should be done this way and not with the |
Yes, that's what I meant.
The types in the new directus SDK do not resolve properly with import { createDirectus, readItems, rest } from '@directus/sdk';
interface Article {
id: number;
title: string;
content: string;
author: Author | null; // <-- this works
// author?: Author; // <-- this doesn't work
}
interface Author {
first_name: string;
}
interface Schema {
articles: Article[]; // <-- Note the array
authors: Author[];
}
// Client with REST support
const client = createDirectus<Schema>('http://directus.example.com').with(
rest(),
);
function fetchArticles() {
return client.request(
readItems('articles', {
fields: ['id', 'title', { author: ['first_name'] }],
}),
);
}
const mockArticles = [
{ id: 1, title: 'Mock Title', author: null },
// ^ errors if you choose `?`
// { id: 2, title: 'Some Title', author: undefined },
// ^ this errors in both cases, so this is not a workaround
{
id: 3,
title: 'Another Title',
author: {
first_name: 'Edgar',
},
},
] satisfies Awaited<ReturnType<typeof fetchArticles>>; |
Is there any progress regarding the array types implementation? |
I have tested it quickly and it does seem to work except for the fields that add "any[]" An example of this is translations fields like the following:
This seems to break the deep filtering. As soon as I removed all of the "any" in the types, the errors went away. |
One more thing. Singletons should not me marked as arrays in the CustomDirectusTypes. |
Ah yes, fixed.
Is that related to the original issue or something else? I originally wrote it like that because, depending on the query, the API either returned a list of primary keys (of which we do not know the type), or complete objects of the type we know. |
@marquetd what exactly needs to change there? |
Thanks, I've updated the example code in 95c53f6 |
In export type CustomDirectusTypes = {
// ...
global_variables: GlobalVariables[];
// ...
}; |
The latest release of the directus SDK version 11 requires the collections to be of array types. See the example code taken from https://docs.directus.io/guides/sdk/getting-started.html#creating-a-composable-client
If the array is missing, you will get type errors when making a call like this:
I propose adding a checkbox
[ ] Generate types for SDK v11
.Thanks for making this extension!
The text was updated successfully, but these errors were encountered: