Is your feature request related to a problem? Please describe.
There is no configuration for TypeScript tuple type
This leads to the error Query has invalid type annotation (SafeQL does not support it. If you think it should, please open an issue)
when I use the following code
async function query() {
type Animal = {
id: number;
};
const [animal1] = await sql<[Animal]>`SELECT * FROM animals WHERE id = 1`;
const [animal2] = await sql<[Animal | undefined]>`SELECT * FROM animals WHERE id = 1`;
}
Describe the solution you'd like
It would be great to have the ability to use tuples
and arrays
together. Maybe adding this configuration to the connections.transform?
eg. The following code should work without throwing an error
async function query() {
type Animal = {
id: number;
};
const animals = await sql<Animal[]>`SELECT * FROM animals`;
const [animal] = await sql<[Animal]>`SELECT * FROM animals WHERE id = 1`;
}
Describe alternatives you've considered
Using it with the array it currently supports and get the single object using array index
eg
async function query(id: number) {
type Animal = {
id: number;
};
const animals = await sql<Animal[]>`SELECT * FROM animals WHERE id = 1`;
return animals[0];
}
But this can lead to an unexpected error, it won't prevent returning undefined
index (eg. return animals[1]
won't throw any error from the above code even though this index will be undefined)
Additional context
It should also have the ability to support union
inside tuple
(eg. [Animal | undefined]
like in the following code)
async function query() {
type Animal = {
id: number;
};
const [animal] = await sql<[Animal | undefined]>`SELECT * FROM animals WHERE id = 1`;
}
I'm sorry but I don't see the added value to support it. How can SafeQL determine when to return an array and when to return a tuple?
If accessing array indexes safely is something important to you, then you can use Array.prototype.at and noUncheckedIndexedAccess
Another option would be to create a wrapper around the sql, for example:
client.queryOneOrNone/queryOne/query which I (personally) believe will be a safer option than a tuple (but of course requires that extra wrapper)
Owner Name | ts-safeql |
Repo Name | safeql |
Full Name | ts-safeql/safeql |
Language | TypeScript |
Created Date | 2022-09-08 |
Updated Date | 2023-03-16 |
Star Count | 795 |
Watcher Count | 5 |
Fork Count | 14 |
Issue Count | 7 |
Issue Title | Created Date | Updated Date |
---|