deepkit-framework icon indicating copy to clipboard operation
deepkit-framework copied to clipboard

Nested `useInnerJoin` typings do not work for optional fields

Open timvandam opened this issue 2 years ago • 2 comments

Example case:

import { Database } from '@deepkit/orm';
import { AutoIncrement, BackReference, PrimaryKey, Reference, Unique } from '@deepkit/type';

class User {
  constructor(public id: number & PrimaryKey & AutoIncrement, public name: string) {}

  userAuth?: UserAuth & BackReference;
}

class UserAuth {
  constructor(
    public id: number & PrimaryKey & AutoIncrement,
    public user: User & Reference & Unique,
    public password: string,
  ) {}

  twoFactorAuth?: TwoFactorAuth & BackReference;
}

class TwoFactorAuth {
  constructor(
    public id: number & PrimaryKey & AutoIncrement,
    public authToken: Buffer,
    public updatedAt: Date,
  ) {}
}

declare const database: Database;

const expiredTwoFactorAuths = database
  .query(User)
  .useInnerJoinWith('userAuth')
  .useInnerJoin('twoFactorAuth') // error: string is not assignable to never
  .filter({ updatedAt: { $lt: Date.UTC(2020, 10, 10) } })
  .end()
  .end()
  .find();

// The type of the second `useInnerJoin` is:
// BaseQuery<(UserAuth & BackReference<{}>) | undefined>
//  .useInnerJoin<never, never>(field: never): JoinDatabaseQuery<never, JoinDatabaseQuery<(UserAuth & BackReference<{}>) | undefined, Query<User>>>

Not sure if its only the typings or also the underlying code. Also happens with useJoin

timvandam avatar Aug 26 '22 20:08 timvandam

The return type of the first useInnerJoinWith is JoinDatabaseQuery<(UserAuth & BackReference<{}>) | undefined, Query<...>>. I think getting rid of the undefined would suffice for fixing this

timvandam avatar Aug 26 '22 20:08 timvandam

Temporary workaround is manually specifying the useInnerJoin generic

timvandam avatar Aug 26 '22 21:08 timvandam