Declaration files (.d.ts) provide type information for JavaScript code without TypeScript source. Generated automatically by tsc --declaration. Written manually for JS libraries without bundled types. Ambient module declarations (declare module "lib" { ... }) add types to untyped modules. Module augmentation extends existing module types (extending Express Request). Global augmentation adds to global namespace (Window, process.env). DefinitelyTyped (@types/lodash) provides community types for popular libraries.
Extending Express Request with custom properties.
declare module 'express' {} in a script file (.d.ts without import/export) creates a new module, not an augmentation. Add import {} from 'express' at the top to make it a module context.
@types/react@18 and react@19 may have type incompatibilities. Check peerDependencies of the @types package. When mismatched, you can use typeRoots to prefer custom types over @types.
declare global { interface Window { gtag: Function } } adds gtag to Window type globally. Must be in a module file (has import/export). Without it, window.gtag is a type error.
Sign in to share your feedback and join the discussion.