Skip to content

Problem with overloads when table is a union type #127

@threema-danilo

Description

@threema-danilo

With ts-sql-query 1.64.0, given these table definitions with compatible columns a and b:

const tFoo = new (class TFoo extends Table<DBConnection, 'TFoo'> {
    public uid = this.autogeneratedPrimaryKey('uid', 'bigint');
    public a = this.optionalColumn('a', 'string');
    public b = this.optionalColumn('b', 'string');
    public constructor() {
        super('foo'); // Table name in the database
    }
})();

const tBar = new (class TBar extends Table<DBConnection, 'TBar'> {
    public uid = this.autogeneratedPrimaryKey('uid', 'bigint');
    public a = this.optionalColumn('a', 'string');
    public b = this.optionalColumn('b', 'string');
    public constructor() {
        super('bar'); // Table name in the database
    }
})();

...I can do the following queries:

// This works
db.selectFrom(tFoo).select({a: tFoo.a, b: tFoo.b}).where(tFoo.a.isNotNull().or(tFoo.b.isNotNull()));
db.selectFrom(tBar).select({a: tBar.a, b: tBar.b}).where(tBar.a.isNotNull().or(tBar.b.isNotNull()));

However, if I try to make this generic:

let table: typeof tFoo | typeof tBar;
if (Math.random() > 0.5) {
    table = tFoo;
} else {
    table = tBar;
}
db.selectFrom(table)
    .select({a: table.a, b: table.b})
    .where(table.a.isNotNull().or(table.b.isNotNull()));

...this is rejected by the type checker:

Argument of type 'BooleanValueSource<TABLE<DB<"DBConnection">, "TFoo">, "required"> | BooleanValueSource<TABLE<DB<"DBConnection">, "TBar">, "required">' is not assignable to parameter of type 'boolean'.
  Type 'BooleanValueSource<TABLE<DB<"DBConnection">, "TFoo">, "required">' is not assignable to type 'boolean'.ts(2345)

Should this be supported? It works without the nested .or, but also causes some other problems where the wrong overloads are picked.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions