0

General:

I'm writing a type declarations file for a Javascript package. The root export is a function that optionally accepts an instance of a third party module. The third party module is not a declared dependency in package.json because I want users to be able to use their preferred version of the third party dependency. I cannot figure out how to specify the type of the argument.

Specific:

Sqorn Postgres is a Javascript library that builds and executes SQL queries. The execution functionality only works if you pass the Node Postgres module as an argument. Node Postgres is NOT a declared dependency in Sqorn Postgres's package.json. Node Postgres has typings on Definitely Typed.

Sqorn Postgres is initialized as follows:

const sqorn = require('sqorn-pg')
const pg = require('pg')
const pool = new pg.Pool()
const sq = sqorn({ pg, pool })

Sqorn Postgres' current Type Declarations exports the following function.

declare function sqorn({ pg, pool }: { pg: any, pool: any }): sqorn.sq;

I'd like to use specific types for the pg and pool arguments, but cannot figure out how.

Eejdoowad
  • 1,297
  • 9
  • 10
  • AFAIK, there isn't a good solution to declare the type of something that comes from an optional dependency, unless the type is very simple and you can just duplicate it in your package. Compare to [this unanswered question](https://stackoverflow.com/q/52795354). The only alternative I see (which may not be worthwhile) is to make a second package (suppose it's called `sqorn-with-pg`) that has `pg` as a required dependency and re-exports `sqorn-pg` with the proper type information and let TypeScript users use `sqorn-with-pg` if they want the type information. – Matt McCutchen Oct 25 '18 at 15:09
  • Thanks for the info and work around Matt. I suppose accepting an any type parameter is preferable to maintaining an additional package. – Eejdoowad Oct 25 '18 at 17:46

0 Answers0