Skip to content

Commit 7b49f94

Browse files
authored
Merge pull request #162 from planetscale/type-cleanup
Constructor type overloading cleanup
2 parents 07a2329 + 26516a3 commit 7b49f94

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ type ExecuteAs = 'array' | 'object'
107107

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

110+
type ExecuteOptions<T extends ExecuteAs = 'object'> = T extends 'array'
111+
? { as?: 'object'; cast?: Cast }
112+
: T extends 'object'
113+
? { as: 'array'; cast?: Cast }
114+
: never
115+
110116
export class Client {
111117
public readonly config: Config
112118

@@ -121,12 +127,12 @@ export class Client {
121127
async execute<T = Row<'object'>>(
122128
query: string,
123129
args?: ExecuteArgs,
124-
options?: { as?: 'object'; cast?: Cast }
130+
options?: ExecuteOptions<'object'>
125131
): Promise<ExecutedQuery<T>>
126132
async execute<T = Row<'array'>>(
127133
query: string,
128134
args: ExecuteArgs,
129-
options: { as: 'array'; cast?: Cast }
135+
options: ExecuteOptions<'array'>
130136
): Promise<ExecutedQuery<T>>
131137
async execute<T = Row<'object'> | Row<'array'>>(
132138
query: string,
@@ -153,12 +159,12 @@ class Tx {
153159
async execute<T = Row<'object'>>(
154160
query: string,
155161
args?: ExecuteArgs,
156-
options?: { as?: 'object'; cast?: Cast }
162+
options?: ExecuteOptions<'object'>
157163
): Promise<ExecutedQuery<T>>
158164
async execute<T = Row<'array'>>(
159165
query: string,
160166
args: ExecuteArgs,
161-
options: { as: 'array'; cast?: Cast }
167+
options: ExecuteOptions<'array'>
162168
): Promise<ExecutedQuery<T>>
163169
async execute<T = Row<'object'> | Row<'array'>>(
164170
query: string,
@@ -224,12 +230,12 @@ export class Connection {
224230
async execute<T = Row<'object'>>(
225231
query: string,
226232
args?: ExecuteArgs,
227-
options?: { as?: 'object'; cast?: Cast }
233+
options?: ExecuteOptions<'object'>
228234
): Promise<ExecutedQuery<T>>
229235
async execute<T = Row<'array'>>(
230236
query: string,
231237
args: ExecuteArgs,
232-
options: { as: 'array'; cast?: Cast }
238+
options: ExecuteOptions<'array'>
233239
): Promise<ExecutedQuery<T>>
234240
async execute<T = Row<'object'> | Row<'array'>>(
235241
query: string,

0 commit comments

Comments
 (0)