Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 8e85991

Browse files
Merge pull request #60 from Microsoft/namespacesDeclMerging
Use 'namespace' instead of 'module' in the 'Declaration Merging' section
2 parents c3b8093 + dac7905 commit 8e85991

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

pages/Declaration Merging.md

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ Declaration merging is not limited to just two declarations, as any number of de
1313

1414
# Basic Concepts
1515

16-
In TypeScript, a declaration exists in one of three groups: namespace/module, type, or value.
17-
Declarations that create a namespace/module are accessed using a dotted notation when writing a type.
18-
Declarations that create a type do just that, create a type that is visible with the declared shape and bound to the given name.
19-
Lastly, declarations create a value are those that are visible in the output JavaScript (e.g. functions and variables).
16+
In TypeScript, a declaration exists in one of three groups: namespace, type, or value.
17+
Declarations that create a namespace are accessed using a dotted notation when writing a type.
18+
Declarations that create a type do just that: create a type that is visible with the declared shape and bound to the given name.
19+
Lastly, declarations that create a value are those that are visible in the output JavaScript (e.g. functions and variables).
2020

2121
| Declaration Type | Namespace | Type | Value |
2222
|------------------|:---------:|:----:|:-----:|
23-
| Module | X | | X |
23+
| Namespace | X | | X |
2424
| Class | | X | X |
25+
| Enum | | X | X |
2526
| Interface | | X | |
27+
| Type Alias | | X | |
2628
| Function | | | X |
2729
| Variable | | | X |
2830

@@ -82,23 +84,23 @@ interface Document {
8284
```
8385

8486

85-
# Merging Modules
87+
# Merging Namespaces
8688

87-
Similarly to interfaces, modules of the same name will also merge their members.
88-
Since modules create both a namespace and a value, we need to understand how both merge.
89+
Similarly to interfaces, namespaces of the same name will also merge their members.
90+
Since namespaces create both a namespace and a value, we need to understand how both merge.
8991

90-
To merge the namespaces, type definitions from exported interfaces declared in each module are themselves merged, forming a single namespace with merged interface definitions inside.
92+
To merge the namespaces, type definitions from exported interfaces declared in each namespace are themselves merged, forming a single namespace with merged interface definitions inside.
9193

92-
To merge the value, at each declaration site, if a module already exists with the given name, it is further extended by taking the existing module and adding the exported members of the second module to the first.
94+
To merge the value, at each declaration site, if a namespace already exists with the given name, it is further extended by taking the existing namespace and adding the exported members of the second namespace to the first.
9395

9496
The declaration merge of `Animals` in this example:
9597

9698
```TypeScript
97-
module Animals {
99+
namespace Animals {
98100
export class Zebra { }
99101
}
100102

101-
module Animals {
103+
namespace Animals {
102104
export interface Legged { numberOfLegs: number; }
103105
export class Dog { }
104106
}
@@ -107,59 +109,59 @@ module Animals {
107109
is equivalent to:
108110

109111
```TypeScript
110-
module Animals {
112+
namespace Animals {
111113
export interface Legged { numberOfLegs: number; }
112114

113115
export class Zebra { }
114116
export class Dog { }
115117
}
116118
```
117119

118-
This model of module merging is a helpful starting place, but to get a more complete picture we need to also understand what happens with non-exported members.
119-
Non-exported members are only visible in the original (un-merged) module. This means that after merging, merged members that came from other declarations can not see non-exported members.
120+
This model of namespace merging is a helpful starting place, but to get a more complete picture we need to also understand what happens with non-exported members.
121+
Non-exported members are only visible in the original (un-merged) namespace. This means that after merging, merged members that came from other declarations can not see non-exported members.
120122

121123
We can see this more clearly in this example:
122124

123125
```TypeScript
124-
module Animal {
126+
namespace Animal {
125127
var haveMuscles = true;
126128

127129
export function animalsHaveMuscles() {
128130
return haveMuscles;
129131
}
130132
}
131133

132-
module Animal {
134+
namespace Animal {
133135
export function doAnimalsHaveMuscles() {
134136
return haveMuscles; // <-- error, haveMuscles is not visible here
135137
}
136138
}
137139
```
138140

139-
Because `haveMuscles` is not exported, only the `animalsHaveMuscles` function that shares the same un-merged module can see the symbol.
140-
The `doAnimalsHaveMuscles` function, even though it's part of the merged Animal module can not see this un-exported member.
141+
Because `haveMuscles` is not exported, only the `animalsHaveMuscles` function that shares the same un-merged namespace can see the symbol.
142+
The `doAnimalsHaveMuscles` function, even though it's part of the merged `Animal` namespace can not see this un-exported member.
141143

142-
# Merging Modules with Classes, Functions, and Enums
144+
# Merging Namespaces with Classes, Functions, and Enums
143145

144-
Modules are flexible enough to also merge with other types of declarations.
145-
To do so, the module declaration must follow the declaration it will merge with. The resulting declaration has properties of both declaration types.
146+
Namespaces are flexible enough to also merge with other types of declarations.
147+
To do so, the namespace declaration must follow the declaration it will merge with. The resulting declaration has properties of both declaration types.
146148
TypeScript uses this capability to model some of patterns in JavaScript as well as other programming languages.
147149

148-
The first module merge we'll cover is merging a module with a class.
150+
The first namespace merge we'll cover is merging a namespace with a class.
149151
This gives the user a way of describing inner classes.
150152

151153
```TypeScript
152154
class Album {
153155
label: Album.AlbumLabel;
154156
}
155-
module Album {
157+
namespace Album {
156158
export class AlbumLabel { }
157159
}
158160
```
159161

160-
The visibility rules for merged members is the same as described in the 'Merging Modules' section, so we must export the `AlbumLabel` class for the merged class to see it.
162+
The visibility rules for merged members is the same as described in the 'Merging Namespaces' section, so we must export the `AlbumLabel` class for the merged class to see it.
161163
The end result is a class managed inside of another class.
162-
You can also use modules to add more static members to an existing class.
164+
You can also use namespaces to add more static members to an existing class.
163165

164166
In addition to the pattern of inner classes, you may also be familiar with JavaScript practice of creating a function and then extending the function further by adding properties onto the function.
165167
TypeScript uses declaration merging to build up definitions like this in a type-safe way.
@@ -169,15 +171,15 @@ function buildLabel(name: string): string {
169171
return buildLabel.prefix + name + buildLabel.suffix;
170172
}
171173

172-
module buildLabel {
174+
namespace buildLabel {
173175
export var suffix = "";
174176
export var prefix = "Hello, ";
175177
}
176178

177179
alert(buildLabel("Sam Smith"));
178180
```
179181

180-
Similarly, modules can be used to extend enums with static members:
182+
Similarly, namespaces can be used to extend enums with static members:
181183

182184
```TypeScript
183185
enum Color {
@@ -186,7 +188,7 @@ enum Color {
186188
blue = 4
187189
}
188190

189-
module Color {
191+
namespace Color {
190192
export function mixColor(colorName: string) {
191193
if (colorName == "yellow") {
192194
return Color.red + Color.green;

0 commit comments

Comments
 (0)