KyselyK
Kysely3y ago
8 replies
autobotkilla

Problems typing a generic withPerson helper

First I want to say that I'm in love with Kysely! It's amazing. I could use some help getting the types right for a helper function I'm creating.

I'm trying to follow the relations recipe (the withPets and withMom example from the docs), but I want to make it slightly more reusable.

I have a withPerson method

export function withPerson<As extends string>(
    builder: ExpressionBuilder<DB, keyof DB>,
    personIdCol: ReferenceExpression<DB, keyof DB>,
    as: As = 'person' as As
) {
    return jsonObjectFrom(
        builder
            .selectFrom('Person')
            .whereRef('Person.id', '=', personIdCol)
            .select([
                'Person.id', 
                'Person.givenName', 
                'Person.familyName', 
                'Person.picture80', 
                'Person.picture144'
            ])
    ).as(as);
}


When I try and use it like this

const result = db
        .selectFrom('Prospect')
        .select((eb) => withPerson(eb, 'person'))
        .execute();

I get a typescript error: Argument of type 'ExpressionBuilder<DB, "Prospect">' is not assignable to parameter of type 'ExpressionBuilder<DB, keyof DB>'
Was this page helpful?