TypeScript declaration merging allows multiple declarations of the same name to be merged into a single definition. This can be done for interfaces, namespaces, enums and other types. Currently, classes can not merge with other classes or with variables.
interface Person {
name: string;
}
interface Person {
age: number;
}
let person: Person = {
name: "John",
age: 30,
};
Some basic rules for declaration merging are:
This is useful when working with third-party libraries or other code that does not provide TypeScript types.
Here’s an example of what the styled.d.ts
declaration file might look like to create custom theme for styled-components:
// import original module declarations
import 'styled-components';
// and extend them!
declare module 'styled-components' {
export interface DefaultTheme {
borderRadius: string;
colors: {
main: string;
secondary: string;
};
}
}