JustTheSyme
JustTheSyme
DTDrizzle Team
Created by JustTheSyme on 1/5/2024 in #help
How to type a function with column key of table
@Angelelz I found that this works fine with typesafety
async function getOptions<
TTable extends Table,
Field extends keyof TTable['_']['columns']
>(table: TTable, field: Field) {
const columns = getTableColumns(table)
const column = columns[field]
if (!column) {
return []
}
const result = await drizzleDb
.selectDistinct({
option: column
})
.from(table)
.orderBy(column)
return result.map((i) => String(i.option))
}
async function getOptions<
TTable extends Table,
Field extends keyof TTable['_']['columns']
>(table: TTable, field: Field) {
const columns = getTableColumns(table)
const column = columns[field]
if (!column) {
return []
}
const result = await drizzleDb
.selectDistinct({
option: column
})
.from(table)
.orderBy(column)
return result.map((i) => String(i.option))
}
But I wish I didn't have to make a call to getTableColumns to make it work. It's not too bad, though.
6 replies
DTDrizzle Team
Created by JustTheSyme on 1/5/2024 in #help
How to type a function with column key of table
@Angelelz I get an error trying to pass column to option with that:
1. Type 'TTable[Field]' is not assignable to type 'SQL<unknown> | Aliased<unknown> | PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}> | PgTable<TableConfig> | SelectedFieldsFlat<...>'.
Type 'TTable[keyof TTable & string]' is not assignable to type 'SQL<unknown> | Aliased<unknown> | PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}> | PgTable<TableConfig> | SelectedFieldsFlat<...>'.
Type 'TTable[string]' is not assignable to type 'SQL<unknown> | Aliased<unknown> | PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}> | PgTable<TableConfig> | SelectedFieldsFlat<...>'.
Type 'TTable[string]' is not assignable to type 'PgTable<TableConfig>'.
Type 'TTable[keyof TTable & string]' is not assignable to type 'PgTable<TableConfig>'.
Type 'TTable[Field]' is not assignable to type 'PgTable<TableConfig>'.
Type 'TTable[keyof TTable & string]' is not assignable to type 'PgTable<TableConfig>'.
Type 'TTable[string]' is not assignable to type 'PgTable<TableConfig>'. [2322]
1. Type 'TTable[Field]' is not assignable to type 'SQL<unknown> | Aliased<unknown> | PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}> | PgTable<TableConfig> | SelectedFieldsFlat<...>'.
Type 'TTable[keyof TTable & string]' is not assignable to type 'SQL<unknown> | Aliased<unknown> | PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}> | PgTable<TableConfig> | SelectedFieldsFlat<...>'.
Type 'TTable[string]' is not assignable to type 'SQL<unknown> | Aliased<unknown> | PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}> | PgTable<TableConfig> | SelectedFieldsFlat<...>'.
Type 'TTable[string]' is not assignable to type 'PgTable<TableConfig>'.
Type 'TTable[keyof TTable & string]' is not assignable to type 'PgTable<TableConfig>'.
Type 'TTable[Field]' is not assignable to type 'PgTable<TableConfig>'.
Type 'TTable[keyof TTable & string]' is not assignable to type 'PgTable<TableConfig>'.
Type 'TTable[string]' is not assignable to type 'PgTable<TableConfig>'. [2322]
6 replies