Improvements to the $queryRaw
function's type signature
#23987
Labels
kind/improvement
An improvement to existing feature and code.
team/client
Issue for team Client.
tech/typescript
Issue for tech TypeScript.
topic: client types
Types in Prisma Client
topic: raw
$queryRaw(Unsafe) and $executeRaw(Unsafe): https://www.prisma.io/docs/concepts/components/prisma-cli
Problem
Using
$queryRaw
currently gives you no type safety at all. This is frustrating in a number of scenarios, where it would be perfectly possible to get the information without compromising on type accuracy.Here are some examples:
There is also some frustration when specifying the type of the rows using the functions type parameters.
$queryRaw
will always return an array, yet it's possible to use the type parameters to specify something that isn't an array:Suggested solution
I propose that we change the function signature as follows:
This would make all the examples above work.
Note that this would be a breaking change, since users of the current type parameter already specifies the extra
Array<...>
that would be superfluous after this change.Alternatives
Another alternativ would be to go one step further, as discussed in #15263. However, my experience with
Record
is that it's not always desired, since it can potentially lead to some problems when trying to do guarded type casts. I think that this would need additional investigation before pursuing:Another alternative would be to remove the ability to specify a type parameter. I actually would prefer this solution since the current disguises a type assertion, and is even listed as a common misstake in the DefinitelyTyped guide.
This is especially problematic for serious organizations that use linting to disallow unsafe type casts, since this usage cannot be checked by the linter.
For current users, this would be slightly more work to update, but the upside would be that they would be made aware of their unchecked type assertions! It would be something like this:
Personally, I think that this would be the best approach. But unfortunately, when maintaining types, I've seen a lot of pushback from users that prefer things to be easy rather than correct. And since this would be a (slightly) larger change for current users, I'm including it as an alternative instead of the main proposal.
Additional context
Related bug: #15263
The text was updated successfully, but these errors were encountered: