fix: make emitted .d.ts self-contained for non-DOM environments#88
Open
YevheniiKotyrlo wants to merge 1 commit intobetter-auth:mainfrom
Open
fix: make emitted .d.ts self-contained for non-DOM environments#88YevheniiKotyrlo wants to merge 1 commit intobetter-auth:mainfrom
YevheniiKotyrlo wants to merge 1 commit intobetter-auth:mainfrom
Conversation
The built .d.ts references Fetch API types (Response, Headers, RequestInit, AbortSignal, URL, Blob, File, etc.) as bare identifiers from lib.dom.d.ts. Consumers whose tsconfig does not include "DOM" in lib get 88 TS2304 "Cannot find name" errors with skipLibCheck: false. Use tsup's dts.banner option to prepend module-scoped Fetch API type stubs to the declaration output. Since the .d.ts files are ES modules (they contain export statements), the declarations are file-scoped: they shadow DOM globals when DOM is present and provide the missing types when it's absent. No global namespace pollution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The built
.d.tsreferences Fetch API types (Response,Headers,RequestInit,AbortSignal,URL,Blob,File, etc.) as bare identifiers fromlib.dom.d.ts. Consumers whose tsconfig does not include"DOM"inlib— a standard configuration for backend projects usinglib: ["ESNext"]— get 88 TS2304 errors whenskipLibCheck: false.This has been reported before in the better-auth ecosystem: better-auth#1550 mentions the
Timertype leak from this package as one of the issues breakingskipLibCheck: falseconsumers.Reproduction
tsconfig.json:
{ "compilerOptions": { "strict": true, "skipLibCheck": false, "moduleResolution": "bundler", "module": "ESNext", "lib": ["ESNext"] } }test.ts:
Fix
Use tsup's native
dts.banneroption to prepend module-scoped Fetch API type stubs to the built.d.tsoutput. Since.d.tsfiles withexportstatements are ES modules, the prepended declarations are file-scoped — they shadow DOM globals when DOM is present (no conflicts) and provide the missing types when DOM is absent. No global namespace pollution.1 file changed (
tsup.config.ts), no new files, no build script changes.Types declared
Minimal stubs covering every Fetch API type referenced in the built output:
RequestCache,RequestCredentials,RequestMode,RequestPriority,RequestRedirect,ReferrerPolicyTimer,HeadersInitReadableStream,Headers,AbortSignal,AbortController,Blob,File,URL,Response,RequestInitdeclare var Blob,declare var File(needed fortypeofusage inoutputoption)declare namespace globalThis { interface Request }(needed forFetchEsquetype)Why this approach
The package uses Fetch API types both as type references (
Response,Headersin type positions) and as runtime globals (new AbortController(),new Headers()). The runtime usage is correct — all modern JS runtimes provide the Fetch API. The problem is only in the type declarations.A source-level ambient
globals.d.tswould pollute the global namespace and potentially conflict with consumers who havelib: ["DOM"]. Module-scoped stubs in the DTS output avoid this entirely.Verification
Verified on v1.1.21 (current npm release):
tsc --noEmit(upstream)pnpm build(tsup)vitest runlib: ["ESNext"]lib: ["ESNext", "DOM"]Note on CI
CI will fail due to a pre-existing build breakage on
main— undefinedheadersvariable inutils.ts(introduced in #65, tracked in #85, fix in #81). This PR does not touchutils.ts.