-
Notifications
You must be signed in to change notification settings - Fork 303
Better sql`` types #84
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
Better sql`` types #84
Conversation
Looks good. |
In my opinion, maybe only set row type to |
I agree but currently it's not possible in TypeScript to transform types in tuples without loosing types positions (i.e. But in the meantime I found another possible solution 😆 The problem is that we loose tuples 😢 |
Partial will work with tuple. And I think tuple usage is more fit in this situation, because most situation we need to handle undefined like this is when we using destructuring with known length of elements, and is same to the usage of tuple. Also just wondering if |
Oh nice that's what I was looking for 👍
What about
It should as tuples are passed as is. 😉 |
That seems reasonable. And next problem is if the usage of I think
|
@yckao So you suggest to drop support for What about a survey 😆? React to this message with:
|
Ok finally I think you're right: const [maybeUser]: [User?] = await sql`SELECT * FROM users WHERE id = ${id}`; // ok
// => maybeUser: User | undefined
// @ts-expect-error
const [invalid] = await sql<42>`SELECT 42`; // fails: `42 not assignable to any[]`
const [valid]: [undefined] = await sql`SELECT null`; // ok but useless
// => valid: undefined But // Tuple mapping now works with the `any[]` constraint on `T`:
// `[42]` mapped to `[{ '?column?': 42 }]`
const [{ "?column?": n }] = await sql<[42]>`SELECT 42`; // ok
// => n: 42
const result = await sql<User[]>`SELECT * FROM users WHERE id = ${id}`; // ok
if (!result.length)
throw new Error('Not found');
return result[0]; // => result[0]: User
const result = await sql<User[]>`SELECT * FROM users WHERE name LIKE ${pattern}`; // ok
return result.map(user => user.id); To conclude, I think the following form is peferable when destructuring: const [user, maybeUser]: [User, User?] = await sql`...`; // ok
// => user: User
// => maybeUser: User | undefined
const [first, second, third]: [User, User?, User] = await sql`...`; // fails
// ^^^^^^
// A required element cannot follow an optional element
const [first, second, third]: [User, ...(User | undefined)[]] = await sql`...`; // ok
// => first: User
// => second: User | undefined
// => third: User | undefined For any other case, |
Only `YourType[]` accepted
YourType | undefined
insql<YourType>``;
but toYourType
insql<YourType[]>``;
so that the user can choose the best regarding the situation (see also Typescript support #8 (comment)) :sql<number>`SELECT 1`;
. This PR addT
to{ '?column?': T }
conversion ifT
isSerializable
(string
,number
,Date
, ...) :Feel free to discuss types here before the release 👍