sqlite3 icon indicating copy to clipboard operation
sqlite3 copied to clipboard

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

Open YievCkim opened this issue 1 year ago • 0 comments

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 ?

YievCkim avatar Jul 23 '24 00:07 YievCkim