prisma icon indicating copy to clipboard operation
prisma copied to clipboard

Narrow type of `select` when filtering

Open Akxe opened this issue 4 years ago • 2 comments

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

Akxe avatar Mar 13 '21 20:03 Akxe

All filtering except column: { eq: null } should result in the column not to be null.

Akxe avatar Oct 10 '23 12:10 Akxe

This has come to bite me as well. Why can't we get the correct type from this simple Prisma select?

shadoath avatar May 20 '24 15:05 shadoath

Related: https://github.com/prisma/prisma/issues/15113 + https://github.com/prisma/prisma/issues/20348 + https://github.com/prisma/prisma/issues/9508

janpio avatar Jun 05 '24 21:06 janpio