Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Add type declarations (resolve #46) #61

Closed
wants to merge 1 commit into from
Closed

Conversation

olavim
Copy link

@olavim olavim commented Feb 22, 2020

Follow-up on #51

@JaneJeon
Copy link
Owner

JaneJeon commented Oct 4, 2020

Hey, I'm really sorry for how freaking LONG it's taken me to get around this (I blame ADHD!). So I tried out your index.d.ts, and while it looks correct, when I look at the index.d.ts in VSCode, I see the same dreaded problem I've been trying to solve in the other PR: overriding SomeModel.query().

It looks like you've tried some fancy stuff above and beyond what the objection docs basically told you to do, and it appears that other than minor nitpicks (e.g. typescript does not pick up import { foo } from 'objection-2', only import {foo} from 'objection' after I've npm i -D objection'd, and some of the imports are unused), it seems like the reason Model.query() still refusing to be typed is because the previous plugin (objection-visibility) also touches QueryBuilder.

At this point, I'm really not sure what to do. As documented in #51 and Vincit/objection.js#1589, TypeScript just seems to shit the bed when it comes to multiple plugins. I'm honestly tempted to just merge this and call it a day, I don't know where to proceed from here. If you have any suggestions for what I should do @olavim, I'd greatly appreciate it.

@JaneJeon
Copy link
Owner

JaneJeon commented Oct 4, 2020

Oh, and one more thing I've noticed (the same with #51 if you're still alive @geopic) - even when I fix aforementioned errors, there are two areas where even this solution falls short:

  1. You don't get any sort of typing on .query(). Sure, typescript won't fail the code, but considering that (at least for me) the whole point of using ts definitions is to get completions on your IDE, the fact that not even
class BaseBaseModel extends plugin(Model) {
    static get tableName() {
      return `table1-${version}`
    }
  }
await BaseModel.query() // <----- here

this gets typed, it's really unfortunate.
2. So while typescript doesn't screech at await BaseModel.query()..., it DOES yell at me for trying await AlgoliaObject.query() or await HiddenId.query(), or hell, even await ModelA.query() (which doesn't even touch any hashId-x properties!) So I'm guessing inheriting from the base class causes SubClass.query() to cause a type error. So the types, again, don't work here :(

A few "fun facts" about the inheritance:

  1. This isn't a problem with multiple plugins touching the QueryBuilder, because even when I set BaseClass as literally just plugin(Model) (i.e. only one layer), I still get the same error.
const modelA = await ModelA.query().insertAndFetch({})

errors, but not

const modelB = await modelA.$relatedQuery('modelBs').insert({})

🤔

@olavim
Copy link
Author

olavim commented Oct 4, 2020

@JaneJeon I don't really remember the minor details anymore, but if Objection's type system and TypeScript's capabilities haven't improved by leaps and bounds, I suggest you give up on this completely. Trying to get proper query builder typings is impossible, and even partial support is a metaphorical hydra: If you solve one issue with the types, you will also create at least one new problem. Even if you assume there are no other plugins, it's impossible to guarantee your types work in all cases given all the trickery you can do with Objection. If there is some other plugin that modifies the query builder, you're straight out of luck.

Heed my advice and don't add types to the query builder. You can add types to plugin options though, if you have those 😄

@JaneJeon
Copy link
Owner

JaneJeon commented Oct 4, 2020

RIP the dreams. Thank you very much for giving this a proper crack, at least!

@JaneJeon JaneJeon self-requested a review October 4, 2020 02:41
@JaneJeon JaneJeon added enhancement New feature or request wontfix This will not be worked on labels Oct 4, 2020
@JaneJeon JaneJeon linked an issue Oct 4, 2020 that may be closed by this pull request
@JaneJeon JaneJeon closed this Oct 4, 2020
@JaneJeon JaneJeon mentioned this pull request Oct 4, 2020
@geopic
Copy link

geopic commented Oct 5, 2020

It was worth a shot.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request wontfix This will not be worked on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeScript type declaration file
3 participants