question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New types in v2 have some issues with single/multiple return types, e.g. with returning("*")

See original GitHub issue

We’re having some issues with typings in 2.0. (v2.1.2 - latest)

Breaking out into multiple issues, I can log separately if needed.

  1. This example from Objection’s Postgres Returning Tricks actually is typed as returning an array - not a single object. So this code in the official Recipe won’t work. (The recipe code may just be wrong since patch().where() seems like it could always return multiple, unless querying for ‘id’ is a specific case in the types that indicates singular return.)
const jennifer = await Person.query()
  .patch({ firstName: 'Jenn', lastName: 'Lawrence' })
  .where('id', 1234)
  .returning('*');
  1. findOne() is typed as returning T, not T | undefined. This means we always have to manually add as T | undefined, as findOne could always fail. In TS there are things that can fail but nevertheless return only T (array indexing for example), but findOne function seems far more analagous to JS array.find() which returns T | undefined, as I believe findOne should. Or, perhaps a tryFindOne function would make sense, which could return undefined.

  2. I’d expect these two queries to both return single User objects:

await User.query().patch({}).findOne({}).returning("*");

await User.query().findOne({}).patch({}).returning("*");

However, the former is typed as returning a single User and the latter returns an array. I’d suggest the latter is the more intuitive usage (find object then patch it) and should return a single User.


I’m sure I’m not aware of all the interactions in the types and whether the changes above are mutually compatible. We’re huge fans of Objection on our team, thanks for all your work contributing this to the JS/TS community!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
koskimascommented, Jan 17, 2020

1 can be fixed by moving findOne/findById/first after returning. There’s no way to make the typings work in every case, believe me, I’ve tried.

2 is an unfortunate tradeoff I had to make to implement some other features a lot of people wanted.

An insane amount of my time went to writing the new typings. You’ll just have to live with the little flaws or write better typings yourself.

0reactions
koskimascommented, Jan 18, 2020

You can add those methods to a custom query builder yourself. Or even override a bunch of methods to return the type you want.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript multiple return types with identical parameters
I have a problem using a method that returns multiple types. If i have my own method that returns CustomType1 from like this:...
Read more >
How to Return Multiple Values From a Java Method - Baeldung
In this tutorial, we'll learn different ways to return multiple values from a Java method. First, we'll return arrays and collections.
Read more >
The Python return Statement: Usage and Best Practices
In describe() , you take advantage of Python's ability to return multiple values in a single return statement by returning the mean, median,...
Read more >
Returning Multiple Values in Python - GeeksforGeeks
In Python, we can return multiple values from a function. ... return Test() ... They are different from arrays as they can contain...
Read more >
Built-in Types — Python 3.11.1 documentation
Some operations are supported by several object types; in particular, ... to a string (with the repr() function or the slightly different str()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found