drizzle-team / drizzle-team/drizzle-orm

[FEATURE]:When I use $type, selectSchema.array().parse and insertSchema.safeParse doesn't get the correct type.

Open
#4,685 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Feature hasn't been suggested before.

- [x] I have verified this feature I'm about to request hasn't been suggested before.

### Describe the enhancement you want to request

```tsx
// App.tsx
import { integer, sqliteTable } from 'drizzle-orm/sqlite-core'
import { createSelectSchema, createInsertSchema } from 'drizzle-zod'
import { openDatabaseSync } from 'expo-sqlite'
import { drizzle } from 'drizzle-orm/expo-sqlite'
import React, { useState } from 'react'

type TUpdateWeekday = 1 | 2 | 3 | 4 | 5 | 6 | 7

export const table = sqliteTable('table', {
updateWeekday: integer('update_weekday').$type().notNull(),
})

export const selectSchema = createSelectSchema(table)
function isTUpdateWeekday(value: number): value is TUpdateWeekday {
return value >= 1 && value <= 7 && Number.isInteger(value)
}
const insertSchema = createInsertSchema(table, {
updateWeekday: (schema) => schema.refine((val) => isTUpdateWeekday(val)),
})

const expo = openDatabaseSync('db.db')
const db = drizzle(expo)

function App() {
type TData = typeof table.$inferSelect
type TInput = typeof table.$inferInsert
const b: any = 2
const a: TInput = {
updateWeekday: b,
}
const result = insertSchema.safeParse(a)
if (result.success) {
console.log(result.data)
db.insert(table).values(result.data)
} else {
console.log(result.error)
}
const [list, setList] = useState([])

async function init() {
const row = await db.select().from(table)
const parseRow = selectSchema.array().parse(row)
setList(parseRow)
}
return


}

export default App
```
![Image](https://github.com/user-attachments/assets/6b4171ad-e8e0-4e44-bcb8-6f071d119eb2)

![Image](https://github.com/user-attachments/assets/7be56fbe-a41d-4d49-b159-4992600c9d47)

![Image](https://github.com/user-attachments/assets/853b19c9-35f8-4274-bcb5-388f57e64a74)

![Image](https://github.com/user-attachments/assets/562440c6-6f77-43d9-9b8d-c06c17013706)

![Image](https://github.com/user-attachments/assets/b09cd669-74f7-4af6-8b5d-df2434b2b030)

I need to use type assertion to resolve this issue.

![Image](https://github.com/user-attachments/assets/07ebea3e-bb85-4f39-b3ad-d79fef976e47)

![Image](https://github.com/user-attachments/assets/30a457aa-60d7-44c9-a869-763200c31501)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.