node-pg-error
node-pg-error copied to clipboard
ES6 Update and TS Support
hello, do you plan update code to es6 and typescript support?
Hey there!
I like some parts of ES6, but I don't think it's reasonable to rewrite something just because. I'm open to someone else writing TypeScript type declarations however. :)
Wrote the declarations, can't guarantee that they're 100% accurate
declare module 'pg-error' {
export = PgError;
class PgError extends Error {
severity: string;
code: string;
condition: string;
detail: string;
hint: string;
position: number;
internalPosition: number;
internalQuery: string;
where: string;
schema?: string;
table?: string;
column?: string;
dataType?: string;
constraint?: string;
file: string;
line: number;
routine: string;
constructor(fields: {});
}
namespace PgError {
let parse: () => {};
}
}
That's great, @KristjanTammekivi. Thanks! I take it they seemed to work for you and the accuracy fear was just modesty? Do those work for you, @cdiaz?
I've used them on several projects in the past year, they should be usable for any end-user use cases (I didn't bother giving accurate properties to the constructor fields parameter since I don't need it)
+1 for including TS definitions in the NPM package. Even without precise types for the constructor fields
parameter, it's still very useful.
For now I've been using @KristjanTammekivi's definitions with a few minor modifications:
- Use
Record<string, unknown>
instead of{}
. typescript-eslint warns against{}
: explanation. - Declare
parse
as a static method instead of a namespaced function, since in the JS code it looks more like a static method. (I don't actually know what the subtle differences are between the two techniques.) - Add types to
parse
.
declare module 'pg-error' {
export = PgError;
class PgError extends Error {
severity: string;
code: string;
condition: string;
detail: string;
hint: string;
position: number;
internalPosition: number;
internalQuery: string;
where: string;
schema?: string;
table?: string;
column?: string;
dataType?: string;
constraint?: string;
file: string;
line: number;
routine: string;
// TODO: A better type for 'fields'.
constructor(fields: Record<string, unknown>);
static parse(buffer: Buffer): PgError;
}
}