findUnique return type does not include null
See original GitHub issueProblem
The docs say findUnique
can return zero or one items that match a query. The issue is that the Typescript return type of findUnique
is simply a promise of an instance of the model you are querying and does not account for when there isn’t a match and the result is null
. This can lead to bugs like Cannot read properties of null (reading 'password')
unless you check the result of findUnique
isn’t null.
See below where user
is typed User
instead of User | null
.
Suggested solution
A setting or something so that I can type the return type of findUnique
to also include null
to force proper type checking. Another alternative is to throw an error if the query doesn’t find a match which is how Django does it. But maybe such a setting exists and it’s not documented?
Alternatives
I know I could just remember to check the output of findUnique
but the whole point of Typescript is so I don’t have to remember things like this.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:6
Top GitHub Comments
I also strongly believe this issue should not be closed. My understanding is that prisma’s main feature is it’s advanced type safety. Here we have a function that returns
null | Type
but does not label itself as such.findUnique
is simply not typed correctly in Prisma andrejectOnNotFound
is a hatch - not a fix.+1
I want a solution where
rejectOnNotFound
is false and the return type offindUnique<T>
isT | null