Skip to content

Why can't Pool be inherited? #1887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tulayang opened this issue May 7, 2019 · 6 comments
Closed

Why can't Pool be inherited? #1887

tulayang opened this issue May 7, 2019 · 6 comments

Comments

@tulayang
Copy link

tulayang commented May 7, 2019

class MyPool extends require('pg').Pool {
  f() {
    console.log('...')
  }
}

var pool = new MyPool()
pool.f()

I got a TypeError:

TypeError: pool.f is not a function
@tulayang
Copy link
Author

tulayang commented May 7, 2019

And the definition of @types/pg-pool is wrong:

/// <reference types="pg" />
export {Pool, PoolConfig } from "pg";

@charmander
Copy link
Collaborator

See #1541.

@tulayang
Copy link
Author

tulayang commented May 7, 2019

@charmander

I see. Would it be a solution to copy the prototype?

var pool = new Pool(config)
var prototype = Object.assign(Reflect.getPrototypeOf(this), Reflect.getPrototypeOf(pool))
Reflect.setPrototypeOf(pool, prototype)
return pool

@charmander
Copy link
Collaborator

I don’t think it should be done with Object.assign. Would a workaround with a direct dependency on pg-pool be acceptable?

const Pool = require('pg-pool')
const {Client} = require('pg')

class MyPool extends Pool {
  constructor(options) {
    super({Client, ...options})
  }

  f() {
    console.log('...')
  }
}

@tulayang
Copy link
Author

tulayang commented May 7, 2019

@charmander

pg-pool should be ok. But the ts definition for pg-pool is wrong. See https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/pg-pool/index.d.ts

That incorrectly refer to pg definition. Therefore, cannot use pg-pool like:

import * as Pool from 'pg-pool'  // error
import Pool from 'pg-pool'       // error
import {Pool} from 'pg-pool'     

console.log(Pool)                // error, not found Pool

@charmander
Copy link
Collaborator

pg’s bound pool (the Pool export) is a class as of 8.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants