deepkit-framework
deepkit-framework copied to clipboard
Nested `useInnerJoin` typings do not work for optional fields
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
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
Temporary workaround is manually specifying the useInnerJoin generic