Declaration Merging
JAGADEESH PATTA ( PJ )
Introduction
 Declaration Merging is the process followed by the compiler
to merge two or more declarations.
 The declarations declared with same name into a single
definition.
Intro…
 A declaration creates entities in at least one of three groups.
Namespace
Type
Value
Namespace
 Creating declarations create a namespace, which contains
names that are accessed using a dotted notation.
namespace namespace_name{
declarations
}
Types
 Creating declarations create a type that is visible with the
declared shape and bound to given name
Eg:
Classes
Interfaces
Enum, Etc.
Values
 Creating declarations create a value that are visible in the
output JavaScript.
Eg:
Classes
Namespace
Function, Variable
Enum, Etc.
Interface Merging
 In interface merging, The merge joins the members of both
declarations into a single interface with same name.
 In interface merging the members must be unique.
 The function members of interface is overloaded.
Interface Merging…
Interface Box{
sayHai();
}
Interface Box{
sayBai();
}
Namespace Merging…
namespace Sample{
export function sayHai(){
console.log(‘hai’);
}
}
namespace Sample{
export function sayBye(){
console.log(‘Bye’);
}
}
Merging namespace with class
class Album{
label: Album.AlbumLabel;
}
namespace Album{
export class AlbumLabel { }
}
Merging namespace with function
Function buildLabel(name){
return buildLabel.prefix + name +
buildLabel.suffix;
}
namespace buildLabel{
export let suffix = "";
export let prefix = "Hello, ";
}
Disallowed Merges
 In Typescript classes can not merge with other classes or with
variables.
Thank You

Declaration merging | Typescript