Vincit / objection.js

An SQL-friendly ORM for Node.js
https://vincit.github.io/objection.js
MIT License
7.26k stars 639 forks source link

TypeScript return type from $relatedQuery is not optional for optional relation #2355

Open ryan-gray-db opened 1 year ago

ryan-gray-db commented 1 year ago

I have a model with a relation that's optional, however when I use $relatedQuery to get the relation, the return type is not optional. I put together a small project to demonstrate!

Snippet from project:

const Example = async () => {
    const customer = await Customer.query().findById("123").throwIfNotFound();
    /**
     * The program relation on customer is an optional relation. I would expect this to be returned from $relatedQuery
     * as optional, but the typing is just Program
     */
    const program = await customer.$relatedQuery("program");
    console.log(program.id);
}

Example project: https://github.com/ryan-gray-db/objection-relation

avetisk commented 3 months ago

Indeed, this is a very annoying issue :/