Hi folks! I need help writing a type that annotates a Javascript variadic function. Maybe this is the wrong angle, so any suggestion is welcomed.
I’m currently writing some bindings for Bun’s SQL API. The SQL constructor returns a function (with some properties, like begin
or unsafe
) that can be interfaced using a template literal. Like this:
const sqlite = new SQL("sqlite://myapp.db");
const sqliteResults = await sqlite`
SELECT * FROM users
WHERE active = ${1}
`;
await sqlite.begin(async tx => {
await tx`INSERT INTO users (name) VALUES (${"Alice"})`;
});
So my bindings look like this:
@variadic // <--- I need T to be variadic
type t = (array<string>, array<'a>) => 'b
@module("bun") @new
external make: string => t = "SQL"
type sqlTransactionContextCallback = t => 'a
@send
external begin: (t, sqlTransactionContextCallback) => promise<'a> = "begin"
But that unfortunately doesn’t actually work (playground)
Currently I have in mind cheating and adding a wrapper function that destrucutures the parameters array in raw js (playground), but I would rather avoid that if possible.
Edit:
Something I forgot to add is that I want the variadic, or the wrapper if need be, so that the DX is as close as possible to JS; with template literals.