Skip to content

Conversation

@varanauskas
Copy link
Contributor

@varanauskas varanauskas commented Feb 15, 2025

Currently some statement methods support generics for type hinting the result of the query, for example:

class Statement {
  all<T extends object = Record<string, any>>(...args: RestBindParameters): T[]
  get<T extends object>(...params: RestBindParameters): T | undefined
}

In a lot of cases the generic used is the same between these two methods because the same query was used to prepare the statement.

I propose adding a generic type to statement itself that would allow to both of these methods to be typed automatically:

interface UserTable {
  name: string;
  email: string;
}
const statement: Statement<UserTable> = new Database().prepare<UserTable>("SELECT name, email FROM users");

const singleUser: UserTable | undefined = statement.get();
const allUsers: UserTable[] = statement.all();

This would lay the groundwork for typing the iterator in the future so it could be used like so:

for (const row of statement) {
  const user: UserTable = row;
}

What remains unsolved is typing of value and values methods, I propose leaving them as is for simplifying the API/proposal

Copy link
Member

@DjDeveloperr DjDeveloperr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for the PR, greatly appreciate it

@DjDeveloperr DjDeveloperr merged commit 1140921 into denodrivers:main May 25, 2025
4 of 5 checks passed
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.

2 participants