denodrivers / sqlite3

The fastest and correct SQLite3 module for Deno runtime
https://jsr.io/@db/sqlite
Apache License 2.0
265 stars 22 forks source link

got TS2344 [ERROR]: Type does not satisfy the constraint 'Record<string, unknown>' using stmt.get #131

Open YievCkim opened 4 months ago

YievCkim commented 4 months ago

I have this table:

CREATE TABLE producers 
   ( id INT UNIQUE NOT NULL
   , title TEXT NOT NULL
   , phone TEXT NOT NULL
   , address TEXT NOT NULL
   , city TEXT NOT NULL
   , zip_code TEXT NOT NULL
   )

And this code produces a typescript error(TS2344):

interface ProducerRecord {
  id: number
  title: string
  phone: string
  address: string
  city: string
  zip_code: string
}

export function fetchProducer(id: number) {
  const stmt = db.prepare(
    'SELECT * FROM producers WHERE id=?;'
  )
  const datum = stmt.get<ProducerRecord>(id)
  if (datum) {
    return {
      ...datum,
      address: datum.address ? datum.address.split('\n') : []
    }
  }
  return null
}

This is the error I get:

error: TS2344 [ERROR]: Type 'ProducerRecord' does not satisfy the constraint 'Record<string, unknown>'.
  Index signature for type 'string' is missing in type 'ProducerRecord'.
  const datum = stmt.get<ProducerRecord>(id)

If I ignore this statement with:


  // @ts-ignore
  const datum = stmt.get<ProducerRecord>(id)

It works.

yet the documentation suggests that this is what is expected .

Did I miss something ?

lil5 commented 2 weeks ago

The deno devs should be using

---get<T extends Record<string, unknown>>(
+++get<T extends Record<string, any>>(
YievCkim commented 2 weeks ago

Ah yes... indeed. I am not an expert, but that makes sense.