Access table field dynamically by an index - help with types

Hello! I'm further extending the DAO from helpHelp with types in Repository Pattern , and currently I'm trying to implement custom ordering. I've already written the code which works, and the only thing I'm missing is type-compliance - accessing table column directly by an index gives me this TS error:
TS2536: Type 'string | number | (keyof TTable["_"]["columns"] & string)' cannot be used to index type 'TTable'.

The code which errors:
interface FieldOrder<TTableFields extends string | number> {
  field: TTableFields;
  order: "asc" | "desc";
}

// This is a method in the class
findAllPaginated(
  page: number,
  pageSize: number,
  orderBy?: FieldOrder<keyof InferSelectModel<TTable>>[],
): Promise<InferSelectModel<TTable>[]> {
  const customOrderBy = orderBy?.map(({ field, order }) => {
    // this part right here
    const sqlField = this.table[field];

    return order === "desc" ? desc(sqlField) : asc(sqlField);
  });
  const defaultOrderBy = [desc(this.sqlCreatedAt)];

  return this.database
    .select()
    .from(this.table)
    .where(this.isNotDeleted)
    .orderBy(...(customOrderBy ?? defaultOrderBy))
    .limit(pageSize)
    .offset(pageSize * (page - 1)) as Promise<InferSelectModel<TTable>[]>;
}


The question is - how can I tell typescript, that this is valid? I'm fine with any type casting solutions, I just don't know how can I take this further.
Was this page helpful?