This TypeScript error occurs when you try to augment an existing module without using the proper `declare module` syntax. Module augmentation allows you to add new declarations to existing modules, but requires specific syntax to work correctly.
Module augmentation in TypeScript is a feature that allows you to add new declarations (types, interfaces, functions, etc.) to existing modules. This is commonly used when you want to extend third-party library types or add custom functionality to built-in modules. The error occurs because TypeScript requires a specific syntax pattern for module augmentation: you must use `declare module "module-name"` followed by your additional declarations. Without this syntax, TypeScript cannot properly merge your new declarations with the existing module.
Change from regular module syntax to declare module syntax:
// Wrong:
module "some-module" {
export interface ExtendedInterface {
newProperty: string;
}
}
// Correct:
declare module "some-module" {
export interface ExtendedInterface {
newProperty: string;
}
}Module augmentations typically belong in .d.ts files or files with declare global. If you're augmenting in a regular .ts file, move the augmentation to a declaration file:
// Create a file: src/types/some-module.d.ts
declare module "some-module" {
// Your augmentations here
}Or use declare global for global augmentations:
declare global {
namespace SomeNamespace {
// Your augmentations here
}
}The module path in your declaration must match how the module is imported. Check for exact string matching:
// If you import like this:
import * as lib from "some-library";
// Your augmentation should be:
declare module "some-library" {
// ...
}
// Not:
declare module "some-library/dist/index" {
// ...
}Use the exact same module specifier that appears in your import statements.
Some modules may not support augmentation if they don't have existing declarations or use specific module systems. Verify the module has existing declarations you can extend:
1. Check if the module has type definitions (look for @types/ package or .d.ts files)
2. Ensure you're not trying to augment a module that uses export = syntax (CommonJS default exports)
3. For global augmentations, ensure you're not conflicting with existing declarations
Different declaration types require different merging approaches:
// Adding to an existing interface:
declare module "some-module" {
interface ExistingInterface {
newMethod(): void;
}
}
// Adding new exports:
declare module "some-module" {
export function newFunction(): string;
}
// Adding to a namespace:
declare module "some-module" {
namespace SubNamespace {
interface NewInterface {
property: boolean;
}
}
}Make sure your augmentation syntax matches the existing module structure.
After fixing the syntax, test that your augmentation works:
1. Create a test file that imports the augmented module
2. Verify TypeScript recognizes your new declarations
3. Check that auto-completion works for the augmented members
4. Ensure no type errors appear when using the augmented functionality
Example test:
import * as lib from "some-module";
// This should now work without type errors
const result = lib.newFunction();
const obj: lib.ExtendedInterface = {
existingProperty: "value",
newProperty: "new value" // This property comes from your augmentation
};Module augmentation follows TypeScript's declaration merging rules. When you use declare module, TypeScript merges your new declarations with the existing module declarations. This is different from module declaration files (.d.ts) that describe entire modules - augmentations only add to existing declarations. For complex augmentations, consider creating a separate augmentation package or using conditional types. Be cautious when augmenting widely-used libraries as your augmentations affect all code that imports that module.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript