Module augmentation lets you add declarations to an existing module's types, typically a third-party package, without forking it. You write declare module 'pkg' { interface Existing { newField: X } } in a file that is itself a module (it has an import or export). The new members merge with the original interfaces via declaration merging. Global augmentation (declare global { … }) does the same for globals such as Window or NodeJS.ProcessEnv. You can add members to interfaces and namespaces, but you can't add new top-level exports or change existing members' types, and type aliases can't be augmented.
Theme typing in a UI library
// mui-theme.d.ts
import '@mui/material/styles';
declare module '@mui/material/styles' {
interface Palette {
brand: Palette['primary'];
}
interface PaletteOptions {
brand?: PaletteOptions['primary'];
}
}
Now theme.palette.brand.main type-checks everywhere.
Request context in a server framework
// types/express.d.ts
import 'express';
declare module 'express-serve-static-core' {
interface Request {
user?: { id: string; roles: string[] };
}
}
Library "registry" interfaces designed for augmentation:
// TanStack Router, i18next, Redux Toolkit and others expose a `Register`-style interface
declare module 'i18next' {
interface CustomTypeOptions {
resources: { common: typeof import('./locales/en/common.json') };
}
}
Libraries use this to give app-specific types to library APIs (route paths, translation keys) without generics on every call.
// env.d.ts
export {};
declare global {
interface Window {
__APP_CONFIG__: { apiUrl: string };
}
namespace NodeJS {
interface ProcessEnv {
DATABASE_URL: string;
}
}
}
Vite projects augment ImportMetaEnv similarly, for import.meta.env.VITE_*.
In a script file (no top-level import or export), declare module 'x' { … } is an ambient module declaration that replaces or defines the module's types rather than augmenting them. Adding import 'x' or export {} turns it into an augmentation that merges. This is the most common reason "my augmentation does nothing" or "suddenly all of the library's types disappeared".
.d.ts file must be part of the compilation (include in tsconfig, or imported somewhere).express-serve-static-core, not express). Check the library's .d.ts.type alias.req.user, for example).declare module 'pkg' merges into existing interfaces, and the file must be a module.declare global for Window/ProcessEnv/ImportMetaEnv.Spotted a mistake or a rights concern? Report an issue