Skip to content

pluck().map() result typed incorrectly #661

@IlyaSemenov

Description

@IlyaSemenov

pluck().map() result is typed incorrectly:

const attributedIds = await db.user.pluck("id").map(id => ({ id, age: 18 }))
console.log(attributedIds)
// ERROR: attributedIds typed as { id: number; age: number; }
// CORRECT: prints [{ id: 1, age: 18 }, { id: 2, age: 18 }]
Full reproduction
// orchid-orm@1.62.3
import { createBaseTable, testTransaction } from "orchid-orm"
import { orchidORM } from "orchid-orm/postgres-js"

export const BaseTable = createBaseTable({ snakeCase: true })

export class UserTable extends BaseTable {
  override readonly table = "user"

  override columns = this.setColumns(t => ({
    id: t.serial().primaryKey(),
    name: t.varchar(),
  }))
}

const db = orchidORM({ log: true }, {
  user: UserTable,
})

async function main() {
  await testTransaction.start(db)
  await db.$query`create table "user" (id serial primary key, name varchar not null)`
  await db.user.insertMany([{ name: "Alice" }, { name: "Bob" }])

  const ids = await db.user.pluck("id") // typed as number[]
  console.log([...ids])
  // CORRECT: ids typed as number[]
  // CORRECT: prints 1, 2

  const attributedIds = await db.user.pluck("id").map(id => ({ id, age: 18 }))
  console.log(attributedIds)
  // ERROR: attributedIds typed as { id: number; age: number; }
  // CORRECT: prints [{ id: 1, age: 18 }, { id: 2, age: 18 }]

  await testTransaction.close(db)
}

main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions