prisma
prisma copied to clipboard
Narrow type of `select` when filtering
Problem
model User {
userID Int @id
name String
alias String?
}
prisma.user.findFirst({
select {
userID: true,
alias: true,
},
where: {
alias: {
not: { equals: null },
},
},
}).then(user => {
// typeof user.alias == 'object' | 'string'
})
Suggested solution
It would be amazing if filtering would make the resulting type more specific.
Alternatives
Manually override the returned type
Additional context
prisma : 2.18.0
@prisma/client : 2.18.0
Current platform : windows
Query Engine : query-engine da6fafb57b24e0b61ca20960c64e2d41f9e8cff1 (at ..\..\node_modules\@prisma\engines\query-engine-windows.exe)
Migration Engine : migration-engine-cli da6fafb57b24e0b61ca20960c64e2d41f9e8cff1 (at ..\..\node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core da6fafb57b24e0b61ca20960c64e2d41f9e8cff1 (at ..\..\node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary : prisma-fmt da6fafb57b24e0b61ca20960c64e2d41f9e8cff1 (at ..\..\node_modules\@prisma\engines\prisma-fmt-windows.exe)
Studio : 0.356.0
All filtering except column: { eq: null } should result in the column not to be null.
This has come to bite me as well. Why can't we get the correct type from this simple Prisma select?
Related: https://github.com/prisma/prisma/issues/15113 + https://github.com/prisma/prisma/issues/20348 + https://github.com/prisma/prisma/issues/9508