Menu

Post image 1
Post image 2
Post image 3
1 / 3
0

declare global vs declare module: When to Use Which

DEV Community·Gabriel Anhaia·25 days ago
#k0p9gl2n
#case#declare#typescript#types#module#file
Reading 0:00
15s threshold

Book: TypeScript in Production — Tooling, Build, and Library Authoring Across Runtimes Also by me: The TypeScript Library — the 5-book collection My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools Me: xgabriel.com | GitHub You've seen this one. An engineer opens a PR adding an auth field to Express.Request . The .d.ts file looks right, and the handler that reads req.auth.userId compiles fine. Twenty minutes after merge, a different service in the same monorepo starts failing the type check with Property 'auth' does not exist on type 'Request' . Same TypeScript version, same tsconfig shape, same @types/express . Different result. The reason is one missing line at the bottom of the .d.ts . An export {} that turns the file into a module, which, in the upside-down world of ambient declarations, is exactly the line that lets declare global reach back out into the global scope on the other end.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More