Skip to content
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

Constructor type overloading cleanup #162

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ type ExecuteAs = 'array' | 'object'

type ExecuteArgs = Record<string, any> | any[] | null

type ExecuteOptions<T extends ExecuteAs = 'object'> = T extends 'array'
? { as?: 'object'; cast?: Cast }
: T extends 'object'
? { as: 'array'; cast?: Cast }
: never

export class Client {
public readonly config: Config

Expand All @@ -121,12 +127,12 @@ export class Client {
async execute<T = Row<'object'>>(
query: string,
args?: ExecuteArgs,
options?: { as?: 'object'; cast?: Cast }
options?: ExecuteOptions<'object'>
): Promise<ExecutedQuery<T>>
async execute<T = Row<'array'>>(
query: string,
args: ExecuteArgs,
options: { as: 'array'; cast?: Cast }
options: ExecuteOptions<'array'>
): Promise<ExecutedQuery<T>>
async execute<T = Row<'object'> | Row<'array'>>(
query: string,
Expand All @@ -153,12 +159,12 @@ class Tx {
async execute<T = Row<'object'>>(
query: string,
args?: ExecuteArgs,
options?: { as?: 'object'; cast?: Cast }
options?: ExecuteOptions<'object'>
): Promise<ExecutedQuery<T>>
async execute<T = Row<'array'>>(
query: string,
args: ExecuteArgs,
options: { as: 'array'; cast?: Cast }
options: ExecuteOptions<'array'>
): Promise<ExecutedQuery<T>>
async execute<T = Row<'object'> | Row<'array'>>(
query: string,
Expand Down Expand Up @@ -224,12 +230,12 @@ export class Connection {
async execute<T = Row<'object'>>(
query: string,
args?: ExecuteArgs,
options?: { as?: 'object'; cast?: Cast }
options?: ExecuteOptions<'object'>
): Promise<ExecutedQuery<T>>
async execute<T = Row<'array'>>(
query: string,
args: ExecuteArgs,
options: { as: 'array'; cast?: Cast }
options: ExecuteOptions<'array'>
): Promise<ExecutedQuery<T>>
async execute<T = Row<'object'> | Row<'array'>>(
query: string,
Expand Down
Loading