Skip to content
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

Add constructor definition for QueryBuilder class for typescript inheritance #2232

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ralcorta
Copy link

To allow inheritance from the QueryBuilder class and use the constructor method, we must add the constructor definition in index.d.ts

@ankitr
Copy link

ankitr commented Mar 12, 2022

Would love to know what the type signature of the constructor is supposed to be. Looks like it's a ModelClass?

@ralcorta
Copy link
Author

ralcorta commented May 27, 2022

Would love to know what the type signature of the constructor is supposed to be. Looks like it's a ModelClass?

Hi @ankitr ! The idea of this is like this:
I was trying to do a BaseQueryBuilder in Typescript to manage the soft delete for all of my models, and I found that I cannot extend de constructor of QueryBuilder class like this:

export class BaseQueryBuilder<M extends Model, R = M[]> extends QueryBuilder<M, R> {

  // ...

  constructor(...args: any[]) {
    super(...args);
    this._handleSoftDelete();
  }

  // ... more code

}

This works on Javascript, but don't work in Typescript.
The solution was add the constructor(...args: any[]): this; on the QueryBuilder class (This change), and that was it.
I've seen several packages out there trying to do it but always in a bit complicated ways and not covering all the cases because they can't add logic in the Query Builder's constructor.

With this constructor typed, I guess people will be able to extend more functionalities to the queryBuilder strategy.

New changes from base project
@ralcorta
Copy link
Author

@ankitr Hello! It was a long time ago, but it's still a good approach. What do you think? I have a package from a fork with this change, good to check.

@ralcorta
Copy link
Author

@ankitr is this a forgotten repo or issue?

@lehni lehni self-assigned this Apr 14, 2023
@lehni lehni added the typings label Apr 15, 2023
@lehni
Copy link
Collaborator

lehni commented Apr 16, 2023

There are a lot of unnecessary formatting changes in this PR, making a review difficult, and I will not merge it in this state. Could you change it so that only the lines that actually need to change are included?

1mike12 added a commit to 1mike12/objection.js that referenced this pull request Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants