Lesson 22 of 56 · Typescript
Declaration Merging & Ambient Declarations
Duration: 20 minutes
Declaration Merging
When you declare the same identifier multiple times, TypeScript merges the definitions.
interface Window { foo: string; }
interface Window { bar: number; }
// Window now has both foo and bar
Merging Functions
function combine(a: string, b: string): string;
function combine(a: number, b: number): number;
function combine(a: any, b: any) {
return a + b;
}
Ambient Declarations (declare)
Declare the shape of a value provided by external script or library.
// globals.d.ts
declare const $: (selector: string) => any;
Now you can use $ without TypeScript complaining.
Module Augmentation
// myLib.d.ts (external)
declare module "my-lib" { export function foo(): void; }
// augmentation
import "my-lib";
declare module "my-lib" { export function bar(): number; }