Skip to content

Commit 08bf89e

Browse files
committed
fix types to use module augmentation instead of @ts-ignore
1 parent 5a2a6aa commit 08bf89e

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

export/httpQuery.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { types as defaultTypes } from '.';
22
import { Socket } from '../shims/net';
33
import { parse } from '../shims/url';
44

5-
// @ts-ignore -- this isn't officially exported by pg
5+
// not officially exported by pg - types declared in pg.internals.types.ts
66
import { prepareValue } from 'pg/lib/utils';
7-
// @ts-ignore -- this isn't officially exported by pg
87
import TypeOverrides from 'pg/lib/type-overrides';
98

109
export class NeonDbError extends Error {

export/index.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import { Socket } from '../shims/net';
33
import { neon, NeonDbError } from './httpQuery';
44
import type { NeonConfigGlobalAndClient } from './neonConfig';
55

6-
// @ts-ignore -- this isn't officially exported by pg
7-
import ConnectionParameters from '../node_modules/pg/lib/connection-parameters';
8-
9-
interface ConnectionParameters {
10-
user: string;
11-
password: string;
12-
host: string;
13-
database: string;
14-
}
6+
import ConnectionParameters from 'pg/lib/connection-parameters';
157

168
/**
179
* We export the pg library mostly unchanged, but we do make a few tweaks.
@@ -290,6 +282,15 @@ function promisify(Promise: any, callback: any) {
290282
return { callback: cb, result: result };
291283
}
292284

285+
// Type augmentation for pg Pool internals that don't have publicly exported types
286+
// - https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
287+
declare module 'pg' {
288+
interface Pool {
289+
options: ConstructorParameters<typeof Pool>[0];
290+
Promise: PromiseConstructorLike;
291+
}
292+
}
293+
293294
class NeonPool extends Pool {
294295
Client = NeonClient;
295296
hasFetchUnsupportedListeners = false;
@@ -302,7 +303,6 @@ class NeonPool extends Pool {
302303
return super.on(event as any, listener);
303304
}
304305

305-
// @ts-ignore -- is it even possible to make TS happy with these overloaded function types?
306306
query(config?: any, values?: any, cb?: any) {
307307
if (
308308
!Socket.poolQueryViaFetch ||
@@ -319,15 +319,11 @@ class NeonPool extends Pool {
319319
}
320320

321321
// create a synthetic callback that resolves the returned Promise
322-
// @ts-ignore -- TS doesn't know about this.Promise
323322
const response = promisify(this.Promise, cb);
324323
cb = response.callback;
325324

326325
try {
327-
const cp = new ConnectionParameters(
328-
// @ts-expect-error -- TS doesn't know about this.options
329-
this.options,
330-
) as ConnectionParameters;
326+
const cp = new ConnectionParameters(this.options);
331327
const euc = encodeURIComponent,
332328
eu = encodeURI;
333329
const connectionString = `postgresql://${euc(cp.user)}:${euc(cp.password)}@${euc(cp.host)}/${eu(cp.database)}`;
@@ -341,7 +337,6 @@ class NeonPool extends Pool {
341337
});
342338

343339
sql(queryText, queryValues, {
344-
// @ts-expect-error -- TS doesn't know about this.options
345340
types: config.types ?? this.options?.types,
346341
})
347342
.then((result) => cb(undefined, result))

export/pg.internals.types.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Typescript module augmentation declarations for pg internals that don't have publicly exported types
2+
// - https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
3+
4+
declare module 'pg/lib/connection-parameters' {
5+
export default class ConnectionParameters {
6+
constructor(config: any);
7+
8+
user: string;
9+
password: string;
10+
host: string;
11+
database: string;
12+
}
13+
}
14+
15+
declare module 'pg/lib/type-overrides' {
16+
export default class TypeOverrides {
17+
constructor(userTypes: any);
18+
19+
getOverrides(format: string): Record<any, any>;
20+
21+
setTypeParser(oid: any, format: string, parseFn: any): void;
22+
23+
getTypeParser(oid: any, format?: string): any;
24+
}
25+
}
26+
27+
declare module 'pg/lib/utils' {
28+
export function prepareValue(value: any): any;
29+
}

0 commit comments

Comments
 (0)