Pouchdb: PouchDB typescript results in TS2351 errors (cannot use 'new' with an expression whose type lacks a call or construct signature)

Created on 4 Oct 2017  路  5Comments  路  Source: pouchdb/pouchdb

I've been trying to use PouchDB with a typescript project, with no luck.

I tried it a couple of ways, some worked fantastically, where I could do new PouchDB<DBTypes.ServerDoc>(DataManager.DB_SERVERS) and get type completion on methods like .allDocs() - All sorts of fun things.

However TypeScript won't generate 'valid' js code: it generates new pouchdb_1.default(DataManager.DB_SERVERS) instead of new pouchdb_1(DataManager.DB_SERVERS)

That was when I use: import PouchDB from 'pouchdb';.

The method I've been using with all my other requires (since I'm converting a small existing project) is import PouchDB = require('pouchdb');, but that gives me this error:

TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.

Overall, I'm very confused on how to use PouchDB property with TypeScript :/

All 5 comments

I did find this git project that seems to suggest the PouchDB typing has a mistake in how it exports it's module.

Having looked into this on the DefinitelyTyped repo, it seems this exact change was made only a month ago.

https://github.com/DefinitelyTyped/DefinitelyTyped/commit/b629f5171e9fa2ab27a586f5d37de9c01edde92a#diff-94ddd632741875069027083e2383de71R27

I applied the code suggested in the pull request, and it fixed my problem, resulting in correctly generated code that worked with auto-completion and type resolving.

So I guess that the PouchDB typescript files should be updated to include this fix?

I don't see any further progress. For everyone struggling the same you can simple patch your typings via postinstall hook. It's ugly but working:

#!/bin/bash

# The types you want to patch without pouchdb- prefix
declare -a types=(
  "adapter-fruitdown"
  "adapter-http"
  "adapter-idb"
  "core"
  "replication"
)

for t in "${types[@]}"
do
  echo "Patching @types/pouchdb-${t}"
  sed -i'' 's/export = /export default /' "node_modules/@types/pouchdb-${t}/index.d.ts"
done

Closing out typescript bugs, we do not use typescript for this repo and if typescript projects cant use other javascript projects then I would suggest that they fix that

I was getting this error until I followed the instructions for setting up PouchDB with Typescript: https://pouchdb.com/guides/setup-pouchdb.html#typescript

After setting "allowSyntheticDefaultImports": true in my typescript compiler options, I no longer saw this error.

Was this page helpful?
0 / 5 - 0 ratings