Type for schema when creating db client
I get the following typescript error whenever I try to include
{ schema }
into my db creation:
```
Type 'LibSQLDatabase<typeof import("server/database/schema")>' is not assignable to type 'BetterSQLite3Database | LibSQLDatabase<Record<string, never>> | null'.
Type 'LibSQLDatabase<typeof import("server/database/schema")>' is not assignable to type 'LibSQLDatabase<Record<string, never>>'.
The types of '.schema' are incompatible between these types.
Type 'ExtractTablesWithRelations<typeof import("server/database/schema")> | undefined' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>> | undefined'.
Type 'ExtractTablesWithRelations<typeof import("server/database/schema")>' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>>'.
Property 'todos' is incompatible with index signature.
Type '{ tsName: "todos"; dbName: "todos"; columns: { id: SQLiteColumn<{ name: "id"; tableName: "todos"; dataType: "number"; columnType: "SQLiteInteger"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, object>; userId: SQLiteColumn<...>; title: SQLiteColumn<....' is not assignable to type '{ tsName: string; dbName: never; columns: never; relations: Record<string, Relation<string>>; primaryKey: AnyColumn[]; }'.
Types of property 'dbName' are incompatible.
Type 'string' is not assignable to type 'never'.```5 Replies
My schema file matches the documentation and is a list of variable exports:
@Titan you need to add the generic param to your
let _db: BetterSQLite3Database | LibSQLDatabase
definition, that's the actual problem
your db variable is being defined without generics which means you're ending up with the default generic param of never
effectively
I believe it's DbType<typeof schema>
but you'll have to look for yourself to confirmthanks for the pointers, no joy but I'll keep digging
? did adding a generic to your declaration not solve it?
since that's the actual problem
the error is reporting that your runtime value can't be assigned to your variable because your variable type has no schema associated with it
not my code but found this as you describe and it works now thanks! https://github.com/andrei-vintila/mogo-sesizeaza/blob/c0bef084e4a0c1467e2cd6c44671a42166514d7a/server/utils/db.ts#L13C1-L13C37