Inferring Types from Schema?

Help plz. Been wrecking my brain why this doesn't work
No description
1 Reply
daveycodez
daveycodezOP3w ago
Type 'keyof ExtractTablesWithRelations<TSchema>' cannot be used to index type 'TSchema extends Record<string, never> ? DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?"> : { [K in keyof ExtractTablesWithRelations<...>]: RelationalQueryBuilder<...>; }'.ts(2536) (property) PgDatabase<NeonHttpQueryResultHKT, TSchema, ExtractTablesWithRelations<TSchema>>.query: TSchema extends Record<string, never> ? DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?"> : { [K in keyof ExtractTablesWithRelations<...>]: RelationalQueryBuilder<...>; } I'm trying to set up an external package to call a generic function to use findMany and findFirst just by passing schema and table name as string. I can only seem to get it to work internally if I use my schema's type, but I want it to be inferred
function test<
TSchema extends Record<string, unknown> = Record<string, never>
>(
db: PgDatabase<PgQueryResultHKT, TSchema>,
table: keyof ExtractTablesWithRelations<TSchema>
) {
return db.query[table].findMany()
}

test(clientDb, "books")
function test<
TSchema extends Record<string, unknown> = Record<string, never>
>(
db: PgDatabase<PgQueryResultHKT, TSchema>,
table: keyof ExtractTablesWithRelations<TSchema>
) {
return db.query[table].findMany()
}

test(clientDb, "books")
how am I supposed to type this ? It's correctly inferring table names for the table parameter perfectly, but the db.query is yelling

Did you find this page helpful?