Type Error When Using ...companies in Drizzle ORM select()

I'm using Drizzle ORM with PostgreSQL and trying to fetch all columns from the companies table along with a job count.

The following code throws a type error (but works at runtime):

const companyResult = await db
  .select({
    ...companies, // Causes a type error
    jobCount: count(jobs.id).as("job_count"),
  })
  .from(companies)
  .leftJoin(jobs, eq(companies.id, jobs.companyId))
  .groupBy(companies.id);


However, explicitly selecting each column works without errors:

const companyResult = await db
  .select({
    id: companies.id,
    name: companies.name,
    logo: companies.logo,
    website: companies.website,
    details: companies.details,
    jobCount: count(jobs.id).as("job_count"),
  })
  .from(companies)
  .leftJoin(jobs, eq(companies.id, jobs.companyId))
  .groupBy(companies.id);


Error:

Argument of type '{ jobCount: SQL.Aliased<number>; _: { readonly brand: "Table"; readonly config: { name: "companies"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "companies"; dataType: "string"; columnType: "PgUUID"; ... 10 more ...; generated: undefined; }, {}, {}>; name: PgColumn<...>; logo: PgColumn<...>; ...' is not assignable to parameter of type 'SelectedFields'.
  Property '_' is incompatible with index signature.
Was this page helpful?