-
Notifications
You must be signed in to change notification settings - Fork 6
Add types declaration file (resolve #46) #51
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@geopic thanks for the contribution. What you added definitely looks great and checks out, but the issue I stated in JaneJeon/objection-authorize#1 (comment) still persists, which is that the QueryBuilder
type is completely erased upon calling Model.query()
...
I know I'm asking for a lot, but would you like to try tackling it? I would really appreciate the help, and I would add this in myself but when I tried pasting in the library author's solution, I had a little bit of a problem with the ambiguity of this
...
Thank you again!
No worries @JaneJeon , I'll tackle it today. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@geopic Hi, how are you verifying these types locally? I've tried everything but I just can't get the types to actually register when I try:
const { Model } = require("objection-2");
const plugin = require(".");
class BaseModel extends plugin(Model) {}
BaseModel.query().insert() // <-- this doesn't work
Have you tried actually calling the plugin and using the returned model with the checkJs
tsconfig option turned on (assuming you have typescript installed)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #51 (review)
@geopic on an unrelated note, has this been a learning experience for you? I just wanna make sure you’re actually getting something out of this, too. |
Thanks for the feedback and tips @JaneJeon . I admit I haven't tried chaining anything in my test .ts file but will give it a shot now. The way Objection plugins have to be written to accommodate Objection and its interesting structure is quite new to me. I can write type declaration files but working with an object that returns a modified instance of itself is quite difficult for me to explicitly define in type files at the moment. However, I am learning. 👍 Fingers crossed you will have a working type declaration file. Another thank you for your patience |
@geopic since it has been a rather lengthy process, I present to you a cheat sheet that you can reference when you're stuck: https://github.com/Vincit/objection.js/blob/master/doc/recipes/plugins.md Here's some more resources that could help you: |
I noticed my issue being referenced here so I felt obliged to throw in my two cents. After trying to tackle this problem for a relatively long time, I'm pretty sure perfect typings aren't possible to be created for Objection plugins at the moment. You can get partial support, but once you use two query builder -modifying plugins, each defining a method that modifies the result type, things break down. I remember pinpointing the issue to the type returned by the custom query building method. Consider methods User.query().custom1().custom2()... The first issue that arises is that when you call This is a pretty small problem group though, and as far as you're fine with partial typing support, then my code here will probably help. |
Thanks for dropping by and letting us know @olavim. |
So I've tried every approach, every way under the sun to get this working. But it is above my skill level at the minute. I'll push a commit fixing the minor details like |
I got the existing test cases to work: https://github.com/olavim/objection-hashid But yeah, Objection's plugin type support is just not there. I have no doubt my code will break immediately after trying some other cases. I had to disable the Moreover, I was unable to change the result type based on the |
Brilliant @olavim! You got it to work as best as it probably could 😛 |
RIP as per #61, but I will leave the issue alive. |
If you have any queries, let me know.