node-pg-error icon indicating copy to clipboard operation
node-pg-error copied to clipboard

ES6 Update and TS Support

Open cdiaz opened this issue 7 years ago • 5 comments

hello, do you plan update code to es6 and typescript support?

cdiaz avatar Sep 08 '17 23:09 cdiaz

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. :)

moll avatar Sep 09 '17 11:09 moll

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: () => {};
    }

}

KristjanTammekivi avatar Aug 27 '19 07:08 KristjanTammekivi

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?

moll avatar Aug 27 '19 10:08 moll

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)

KristjanTammekivi avatar Aug 28 '19 06:08 KristjanTammekivi

+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:

  1. Use Record<string, unknown> instead of {}. typescript-eslint warns against {}: explanation.
  2. 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.)
  3. 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;
    }
}

cakoose avatar Jan 17 '21 19:01 cakoose