New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
slonik: QueryResultRowColumnType
is too tight to use .oneFirst() on a SELECT EXISTS(...)
query
#41477
Comments
This is also a problem when using custom const pool = createPool('postgresql://', {
typeParsers: [],
}); In this case It seems that this kind of mimics the Flow type in Slonik so I'll create an issue there too: |
I commented on the issue in Slonik that jgonera opened, but I agree, this type should really be any - having it be anything other than any gives an incorrect false sense of type safety. The default type parsers already parse JSONB fields as deserialized javascript objects, so column results can really be anything. Also, from just a pure "ideology" perspective, my 2c is that it is better to leave query results essentially untyped. Right now I always do something like this:
While I could add a type to queryResult based on the column definition in my SQL, in my opinion that's a mistake because it's too easy to change the SQL string and not update the type definition. There is really no way to automatically know what the types are unless you take an approach like https://github.com/mmkal/slonik-tools/tree/master/packages/typegen and generate the types by watching the program as it executes (I think that @slonik/typegen package is super innovative but I'm not sure how I feel yet about making types be dependent on the runtime results). |
Agreed that Unfortunately changing to |
I don't feel super strongly on this one, as I can always just cast to I understand this is more of a philosophical issue, so again I could go either way, but in general I think that APIs that return copious amounts of unknowns, where those unknowns are always expected to be narrowed, result in a poor developer experience. |
@adevine that's a good point - it's possible that the type tooling could improve to mitigate unknowns in general, but for now this could be annoying. In the meatime, if we did use import {sql as originalSql, TaggedTemplateLiteralInvocationType, createPool} from 'slonik'
export interface TypedSql<T> {
(...params: Parameters<typeof originalSql>): TaggedTemplateLiteralInvocationType<T>
}
test('typed sql function', async () => {
const sql: TypedSql<Record<string, any>> = originalSql
const slonik = createPool('')
const result = await slonik.one(sql`select * from foo`)
result
}) Then the types work the way you're suggesting here. You could also replace This is basically a non-automated version of what @slonik/typegen does. |
Hi thread, we're moving DefinitelyTyped to use GitHub Discussions for conversations the To help with the transition, we're closing all issues which haven't had activity in the last 6 months, which includes this issue. If you think closing this issue is a mistake, please pop into the TypeScript Community Discord and mention the issue in the |
Authors: @sebald @mmkal
in my code, I use a query like:
to get a simple true-or-false answer about whether any rows match the condition. this method returns a boolean value with the default set of type parsers. however,
QueryResultRowColumnType
is defined asstring | number
, so I have to cast around it with something like:not sure if just loosening the definition to
string | number | boolean
is a good idea, wanted to put this out for feedback before opening a pull request.The text was updated successfully, but these errors were encountered: