klaemo opened a new issue, #381:
URL: https://github.com/apache/couchdb-nano/issues/381
## Expected Behavior
A TypeScript project that depends on `nano` (and not on `undici`) should
typecheck with `skipLibCheck: false`. `agentOptions` should keep a real type
when `skipLibCheck: true` is set.
## Current Behavior
`lib/nano.d.ts` line 23 has `import undici from "undici"`, but
`package.json` lists `undici` only under `devDependencies`, so consumers don't
install it.
- With `skipLibCheck: false`:
```
node_modules/nano/lib/nano.d.ts(23,20): error TS2307: Cannot find module
'undici' or its corresponding type declarations.
```
- With `skipLibCheck: true`, the error is hidden, but the import resolves to
`any`, and `agentOptions` with it. `nano({ url, agentOptions: 42 })` typechecks.
pnpm makes this more likely because undici isn't hoisted with an isolated or
global virtual store. With npm, undici is only resolvable if some other package
happens to pull it in.
## Possible Solution
Some options. I'm happy to send a PR for whichever you prefer:
1. **Optional peer dependency.** Add `"peerDependencies": { "undici": ">=6"
}` and `"peerDependenciesMeta": { "undici": { "optional": true } }`. This
documents that a custom agent requires undici (the migration guide already says
that). Package managers then don't warn when it's missing, and pnpm links it
when the app has it. TS2307 remains for consumers without undici and
`skipLibCheck: false`.
2. **Structural type without the import.** Declare a minimal
`Dispatcher`-like interface in `nano.d.ts`, for example `{ dispatch(options:
object, handler: object): boolean }`, and drop the import. Consumers won't need
undici types at all, and `Agent`/`MockAgent` instances still match
structurally. The trade-off is weaker typing on `agentOptions`.
3. **`import type { Dispatcher } from "undici-types"`.** `undici-types` is a
dependency of `@types/node`, which `nano.d.ts` already references, and it
describes the undici bundled in Node, the one whose global `fetch` nano calls.
However, it's only a transitive dependency, so with pnpm it isn't guaranteed to
resolve from nano's directory unless nano declares it. That means adding
`undici-types` to `dependencies`, which is types-only and small. Instances from
a separately installed `undici` might also differ slightly from `undici-types`
across versions.
Option 3 with `undici-types` in `dependencies`, or option 2, would fix the
TS2307 for everyone. Option 1 mainly makes the requirement visible.
## Steps to Reproduce (for bugs)
1. `npm init -y && npm i [email protected] typescript @types/node` (no undici)
2. `tsconfig.json`: `{ "compilerOptions": { "module": "nodenext",
"moduleResolution": "nodenext", "strict": true, "noEmit": true, "skipLibCheck":
false } }`
3. `index.ts`: `import Nano from 'nano'; Nano({ url: 'http://localhost:5984'
})`
4. `npx tsc -p .` gives TS2307 for `undici` in
`node_modules/nano/lib/nano.d.ts`. With `skipLibCheck: true`, `Nano({ url,
agentOptions: 42 })` compiles.
## Context
Found while upgrading an app to nano 11.0.7 with pnpm. This applies to the
`undici` import both on `main` and after the proposed `agentOptions` type fix
in #380.
## Your Environment
* Version used: nano 11.0.7 (`main` at 53dea92), TypeScript 6.0.3
* Node.js v24.21.0, pnpm
* Operating System and version: Linux
* Link to your project: private app
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]