Skip to content

Commit 318ecb3

Browse files
thebearingedgeporsager
authored andcommitted
Minor typescript organization (#416)
* types: downgrade JSToPostgresTypeMap * This type is better represented as a Record<string, unknown> and defaulting to an empty record. * types: change PostgresTypeList to Record. fixes #448 * replace PostgresTypeList interface with inline type constraints * types: allow explicit undefined parameter for options argument
1 parent f254c23 commit 318ecb3

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

types/index.d.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Readable, Writable } from 'node:stream'
55
* @param options Connection options - default to the same as psql
66
* @returns An utility function to make queries to the server
77
*/
8-
declare function postgres<T extends PostgresTypeList>(options?: postgres.Options<T> | undefined): postgres.Sql<PostgresTypeList extends T ? {} : { [type in keyof T]: T[type] extends {
8+
declare function postgres<T extends Record<string, postgres.PostgresType> = {}>(options?: postgres.Options<T> | undefined): postgres.Sql<Record<string, postgres.PostgresType> extends T ? {} : { [type in keyof T]: T[type] extends {
99
serialize: (value: infer R) => any,
1010
parse: (raw: any) => infer R
1111
} ? R : never }>
@@ -15,15 +15,15 @@ declare function postgres<T extends PostgresTypeList>(options?: postgres.Options
1515
* @param options Connection options - default to the same as psql
1616
* @returns An utility function to make queries to the server
1717
*/
18-
declare function postgres<T extends PostgresTypeList>(url: string, options?: postgres.Options<T> | undefined): postgres.Sql<PostgresTypeList extends T ? {} : { [type in keyof T]: T[type] extends {
18+
declare function postgres<T extends Record<string, postgres.PostgresType> = {}>(url: string, options?: postgres.Options<T> | undefined): postgres.Sql<Record<string, postgres.PostgresType> extends T ? {} : { [type in keyof T]: T[type] extends {
1919
serialize: (value: infer R) => any,
2020
parse: (raw: any) => infer R
2121
} ? R : never }>
2222

2323
/**
2424
* Connection options of Postgres.
2525
*/
26-
interface BaseOptions<T extends PostgresTypeList> {
26+
interface BaseOptions<T extends Record<string, postgres.PostgresType>> {
2727
/** Postgres ip address[s] or domain name[s] */
2828
host: string | string[] | undefined;
2929
/** Postgres server[s] port[s] */
@@ -124,13 +124,6 @@ interface BaseOptions<T extends PostgresTypeList> {
124124
keep_alive: number | null;
125125
}
126126

127-
interface PostgresTypeList {
128-
[name: string]: postgres.PostgresType;
129-
}
130-
131-
interface JSToPostgresTypeMap {
132-
[name: string]: unknown;
133-
}
134127

135128
declare const PRIVATE: unique symbol;
136129

@@ -332,7 +325,7 @@ declare namespace postgres {
332325
[name: string]: string;
333326
}
334327

335-
interface Options<T extends PostgresTypeList> extends Partial<BaseOptions<T>> {
328+
interface Options<T extends Record<string, postgres.PostgresType>> extends Partial<BaseOptions<T>> {
336329
/** @inheritdoc */
337330
host?: string | undefined;
338331
/** @inheritdoc */
@@ -364,7 +357,7 @@ declare namespace postgres {
364357
timeout?: Options<T>['idle_timeout'] | undefined;
365358
}
366359

367-
interface ParsedOptions<T extends JSToPostgresTypeMap> extends BaseOptions<{ [name in keyof T]: PostgresType<T[name]> }> {
360+
interface ParsedOptions<T extends Record<string, unknown> = {}> extends BaseOptions<{ [name in keyof T]: PostgresType<T[name]> }> {
368361
/** @inheritdoc */
369362
host: string[];
370363
/** @inheritdoc */
@@ -531,7 +524,7 @@ declare namespace postgres {
531524
| boolean
532525
| Date // serialized as `string`
533526
| readonly JSONValue[]
534-
| { toJSON(): any } // `toJSON` called by `JSON.stringify`; not typing the return type, typings is strict enough anyway
527+
| { toJSON(): any } // `toJSON` called by `JSON.stringify`; not typing the return type, types definition is strict enough anyway
535528
| {
536529
readonly [prop: string | number]:
537530
| undefined
@@ -637,14 +630,14 @@ declare namespace postgres {
637630
type Fragment = PendingQuery<any>
638631

639632
type ParameterOrJSON<T> =
640-
| SerializableParameter<T>
641-
| JSONValue
633+
| SerializableParameter<T>
634+
| JSONValue
642635

643636
type ParameterOrFragment<T> =
644-
| SerializableParameter<T>
645-
| Fragment
637+
| SerializableParameter<T>
638+
| Fragment
646639

647-
interface Sql<TTypes extends JSToPostgresTypeMap> {
640+
interface Sql<TTypes extends Record<string, unknown> = {}> {
648641
/**
649642
* Query helper
650643
* @param first Define how the helper behave
@@ -699,7 +692,7 @@ declare namespace postgres {
699692
prepare?: boolean | undefined;
700693
}
701694

702-
interface TransactionSql<TTypes extends JSToPostgresTypeMap> extends Sql<TTypes> {
695+
interface TransactionSql<TTypes extends Record<string, unknown> = {}> extends Sql<TTypes> {
703696
savepoint<T>(cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>;
704697
savepoint<T>(name: string, cb: (sql: TransactionSql<TTypes>) => T | Promise<T>): Promise<UnwrapPromiseArray<T>>;
705698
}

0 commit comments

Comments
 (0)