Skip to content

Typescript signature of with() does not seem correct when function is provided as second argument #1999

Description

@dschreij

Environment

  • Operating System: Linux
  • Node Version: v22.14.0
  • Nuxt Version: 3.16.2
  • CLI Version: 3.24.1
  • Nitro Version: 2.11.8
  • Package Manager: pnpm@10.8.0
  • Builder: -
  • User Config: compatibilityDate, future, devtools, imports, watch, runtimeConfig, ssr, plugins, modules, auth, lodash, i18n, vuetify, piniaOrm, pinia, css, eslint
  • Runtime Modules: @nuxt/eslint@1.3.0, @nuxt/image@1.10.0, @nuxt/scripts@0.11.5, @nuxt/test-utils@3.17.2, @nuxt/test-utils/module@3.17.2, @nuxtjs/i18n@9.4.0, @pinia/nuxt@0.10.1, @pinia-orm/nuxt@1.10.2, @sidebase/nuxt-auth@0.10.1, @vueuse/nuxt@13.0.0, @zadigetvoltaire/nuxt-gtm@0.0.13, nuxt-lodash@2.5.3, vuetify-nuxt-module@0.18.5
  • Build Modules: -

Reproduction

Hard to show, as it seems to be a TypeScript issue.

export class Shipment extends Model {
  static override entity = 'shipments'

  @Num(0) declare id: number
  @HasMany(() => Review, 'shipment_id') declare reviews?: Review[]
}

export class Review extends Model {
  // This is the name used as module name of the pinia Store.
  static override entity = 'reviews'
  
  @Num(0) declare id: number
  @Num(0) declare shipment_id: number
}

export class ShipmentRepository extends Repository<Shipment> {
  override use = Shipment

  get(id: string | number, role = UserRole.CLIENT) {
     const q = this.query()
         .where((shp: Shipment) => shp.id === Number.parseInt(id.toString()))
         .with('reviews', (query) => {  // <-- Not accepted  as expected signature is () => void
             if (role === UserRole.CLIENT) {
                 query.where('user_role', role)
             }
         })
  }
}

Describe the bug

According to the documentation on constraining a relationship query, you should be able to provide a second argument to .with() that is a function receiving the Query instance. However, TypeScript does not agree to this:

Image

Inspecting the type signature of with(), you see the following:

callback?: M[T] extends Model | Model[] | null ? EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> : () => void): this;

Somehow, it indeed appears the function is incorrectly resolved to () => void, because EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> is for some reason not satisfied

Additional context

No response

Logs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions