How to add custom.d.ts interfaces of npm module in a library and make it available for other projects

6 days ago 8
ARTICLE AD BOX

Consumers won’t see it unless the augmentation ships with the package and TypeScript in the consuming project actually includes that .d.ts in its compilation.

Put the augmentation in your library and ensure it’s published

Example layout:

api/ src/ index.ts types/ express-augment.d.ts package.json

Expose it as a subpath in package.json

{ "name": "@org/api", "types": "./dist/index.d.ts", "exports": { ".": { "types": "./dist/index.d.ts", "default": "./dist/index.js" }, "./express-augment": { "types": "./dist/express-augment.d.ts" } }, "files": ["dist"] }

In each consuming project (JS or TS), add one import

Somewhere that runs once (app entrypoint):

import "@org/api/express-augment";

Even in JS projects, you can add it in a .d.ts shim file.

That’s it. Now all projects “see” req.user.

Read Entire Article