Skip to content

Conversation

dfahlander
Copy link

@dfahlander dfahlander commented Sep 26, 2025

Resolving feature issue #886

using is really neat and would be easily introduced. The only tricky part is to introduce it so that both the type system and the runtime adapts, compiles and runs no matter the typescript library versions.

This would allow the following code:

using reserved = await sql.reserve()
const users = await reserved`select * from users`
return users;

...instead of the equivalent:

using reserved = await sql.reserve()
try {
  const users = await reserved`select * from users`
  return users;
} finally {
  reserved.release()
}

This PR does the following:

  1. Typings: If typescript knows about Symbol.dispose, extends ReservedSql interface to have Symbol.dispose.
  2. Runtime: polyfill Symbol.dispose if not availabe in runtime.
  3. Attach a Symbol.dispose method on reserved sql instances that is equivalent to release().

Q/A

Q: Why not Symbol.asyncDispose() ?
A: Would only be needed if release() was async and it's not. This PR only supports using conn = sql.reserve(), not await using conn = sql.reserve() because sql.release() returns void according to both typings and the runtime code.

Q: How could it be tested?
A: Typescript will transpile using according to the target, so there's no need to use a modern javascript engine. Just make sure to use a modern version of typescript and ES library:

  • Use Typescript 5.2 or later
  • In tsconfig, use "lib": ["ES2022"] (or later)
  • In the code, use using conn = sql.reserve() instead of having to try...finally...release().

Q: Is this a typescript-only thing?
A: No, this is a TC39 stage 3 proposal, so it will soon be out in standard javascript. Until implemented in major JS engines, typescript or babel handles the transpilation. This PR does only declare the reserved sql instance to comply with this.

Q: Is the PR ready for release()
A: No, I thing the author @porsager need to review, build and test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant