Improve TypeScript experience in the SDK #12784
Replies: 6 comments
-
Totally! We should add a (@joelbqz) |
Beta Was this translation helpful? Give feedback.
-
|
Hi all, is there any update or workarounds on "Partials" and "Fields filter"? I am facing exactly the described problem that each field could also be "undefined" although I know that it is not. This makes working with the SDK exhausting, as I have to either cast the types or write an if-statement before each use so that TypeScript doesn't throw errors. |
Beta Was this translation helpful? Give feedback.
-
|
This is on our backlog but we didn't have the time yet to finalise. If you have an idea of how can we solve this, we are open for a PR 👍 |
Beta Was this translation helpful? Give feedback.
-
|
Hey @joselcvarela, thank you for your answer. Unfortunately, I can't accommodate that in terms of time. I will support you in other ways this year. |
Beta Was this translation helpful? Give feedback.
-
|
I think it would require some major changes to the sdk to make it type safe. The current way of passing one magic string means that ts can't help you at all at writing it and cannot infer any return types from it. If the api was more like mongodb api or similar, it could be typed properly. For example if you had something like instead of |
Beta Was this translation helpful? Give feedback.
-
|
Heya! Thank you for taking the time to submit this request! It has been over 90 days, and this discussion has not received at least 15 votes from the community. This means that we don't feel like there's enough community interest to warrant further R&D into this topic at this time. 🧊 This request will now be closed to keep our discussions tidy. Please reach out if you have any questions! For more information, see our Feature Request Process. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Preflight Checklist
Describe the Bug
The types in the SDK are not optional and can be improved.
Collections querying
At the moment, the type of the collection (C) accepts a pure string and returns a collection, based on that string, as defined in the instance type (T):
While this works well for return type, it doesn't have an autocomplete for the collection names. This has an easy fix:
Partials
OneItem and ManyItems["data"] are both defined as Partials, which I believe is due to the fields selection that's available in the SDK. However, this makes the usage of the library very difficult for the end user. Perhaps a better way would be to look at the fields and decide on the type. Not sure of the proper implementation, but should be possible (see the next point for similar solution).
Fields filter
Fields are prompted properly today. However, as of TypeScript 4.1, there is now support for Template Literal Types, so we can also describe nested fields safely. Here is an article about it - https://newbedev.com/typescript-deep-keyof-of-a-nested-object
Side note: Maybe we could also create a d.ts generator to generate definitions from a running Directus Instance 😛
What version of Directus are you using?
rc.92
Beta Was this translation helpful? Give feedback.
All reactions