-
Notifications
You must be signed in to change notification settings - Fork 638
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
Typings: allow $relatedQuery to be strongly typed without a cast #709
Conversation
Dang. You are a master of Typescript type definitions. I don't even understand what you just did, will study it hoping to learn something. Thanks! |
@jtlapp : The By putting this new definition first, it will be applied if it can, and if TypeScript can't find a field with that name, it reverts to the old signature that supports a typecast. Read more here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html |
Maybe we also need this for the new static |
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.
Super cool type sorcery!
@jtlapp the static |
@mceachen People seem to use |
This diff shouldn't change any of the I don't believe I can do anything additional if I can't mandate that people should specify relation fields in their models. The thing is, at least with TypeScript, model classes are pretty useless if you don't add the relation fields to them--you'd have to fight TypeScript every time you'd want to reference a relation. Perhaps I'm being too flexible? @koskimas, what do you think, can we make this assertion? I think if we did, it would unlock additional |
Yes, I realise that. What I meant was how will the new const result: SomeModel[] = model.$relatedQuery('somethings'); it's enough if the following still works, since that's the way it has to be done currently: const result: SomeModel[] = model.$relatedQuery<SomeModel[]>('somethings'); |
@mceachen, would you mind giving me an example of such a cast? The following: const actors = <Person[]>(await movie.$relatedQuery('actors')); Gives me this error:
I guess this works, but it offers no type-safety: const actors = <Person[]><any>(await movie.$relatedQuery('actors')); |
const actors = await movie.$relatedQuery<Person>('actors'); Examples are in |
Okay in that case we just have a miscommunication. I call that type parameterization and did point out that possible solution. |
@mceachen, I'm still getting less-than-perfect behavior from return person.$relatedQuery('movies', trx).insert({name: 'The Last Jedi'}); Gives me this error:
The error goes away when I do either of the following, where MovieModel extends Model: return person.$relatedQuery('movies', trx).insert(<MovieModel>{name: 'The Last Jedi'}); return person.$relatedQuery<MovieModel>('movies', trx).insert({name: 'The Last Jedi'}); Am I misunderstanding something here? |
I guess there's no way for |
It seems that this new typing mainly helps when inserting Model instances? I think I prefer the original typing of |
I backed out this The solution is to call This is just a case of getting a misleading error. Nothing is wrong with the parameter, despite the error saying so. |
My apologies for that. This new problem also existed prior to the mod. The mod's looking good. |
This diff adds an override to
$relatedQuery
such that if your Model subclass to has the field defined, the result type of the QueryResult can be inferred.Prior code that uses a cast will still work.
Related: question 2 at #708 (comment)