Although I can create typings that allow my typescript modules to consume function definitions from a third-party javascript module, this seems to create problems in turn for packages importing my module.
I populated a global.d.ts file to add the following typings for my typescript project since pouchdb-collate is a pure javascript project with no @types package...
// global.d.ts
declare module "pouchdb-collate" {
export function toIndexableString(item: any): string;
export function parseIndexableString(indexableString: string): any;
}
This works well in my monorepo within project A which uses pouchdb-collate and has the global.d.ts
in its src/
. However, whenever I import project A into project B in monorepo, the typings of pouchdb-collate are then unavailable to the project B, which results in an error like...
../collate/src/index.ts:4:8 - error TS7016: Could not find a declaration file for module 'pouchdb-collate'. '/project/blabla/node_modules/.pnpm/pouchdb-collate@7.2.2/node_modules/pouchdb-collate/lib/index.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/pouchdb-collate` if it exists or add a new declaration (.d.ts) file containing `declare module 'pouchdb-collate';`
That is even though there is already a global.d.ts file in the collate/src
folder containing exactly that syntax, and which provided the correct types when running scripts from within the folder!
I can only fix this by placing another copy of global.d.ts
in project B as well.
As an alternative I have tried to create a package C within the monorepo that contains a global.d.ts
and reexports the symbols from pouchdb-collate
via an index.ts
file like...
// modules/collate/src/index.ts
export { parseIndexableString, toIndexableString } from "pouchdb-collate";
The benefit of this approach (aliasing via a package) would be to eventually replace those functions with versions vendored in, rewritten and tested in typescript. However, this approach suffers from the same issue.
What is the way for package C to re-export the symbols AND suitable typings for this third-party package so that projects importing package C can consume them?
For my case I succeeded at re-exporting the third-party module with typings as below.
First I created a new module in my monorepo called @blabla/collate that imported and re-exported in pure javascript like...
// src/index.js
module.exports = require("pouchdb-collate");
Next to it I placed a file called index.d.ts
declaring types for my new package...
// src/index.d.ts
declare module "@blabla/collate" {
export function toIndexableString(item: any): string;
export function parseIndexableString(indexableString: string): any;
}
As recommended by the typescript docs, I then added values to the package.json
of @blabla/collate
as follows...
"main": "src/index.js",
"types": "src/index.d.ts",
So this doesn't directly type the pouchdb-collate
package, but it does re-export via normal javascript mechanisms, then exports declarations for the new javascript source file.
Feels like there should be a more elegant approach so I won't mark this as the answer yet in case anyone has a contribution to make.