Skip to content

Commit 1d9f942

Browse files
committed
added tips on declaration spaces
1 parent 80dc2ac commit 1d9f942

File tree

3 files changed

+90
-9
lines changed

3 files changed

+90
-9
lines changed

code/declarationspaces/declarationspace.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,45 @@ var fourn;
3232
var meh;
3333
(function (meh) {
3434
var something = {};
35-
(function (something) {
36-
something.foo = 123;
37-
})(something || something, {});
3835
})(meh || (meh = {}));
3936
var utility;
4037
(function (utility) {
4138
function log(msg) {
4239
console.log(msg);
4340
}
41+
utility.log = log;
4442
function error(msg) {
4543
console.error(msg);
4644
}
45+
utility.error = error;
4746
})(utility || (utility = {}));
4847
utility.log('Call me');
4948
utility.error('maybe!');
49+
var importing;
50+
(function (importing) {
51+
var Foo = (function () {
52+
function Foo() {
53+
}
54+
return Foo;
55+
})();
56+
var Bar = Foo;
57+
var bar;
58+
})(importing || (importing = {}));
59+
var importing;
60+
(function (importing) {
61+
var Foo = (function () {
62+
function Foo() {
63+
}
64+
return Foo;
65+
})();
66+
importing.Foo = Foo;
67+
})(importing || (importing = {}));
68+
var Bar = importing.Foo;
69+
var bar;
70+
var typeofAnnotation;
71+
(function (typeofAnnotation) {
72+
var foo = 123;
73+
var bar;
74+
bar = 456;
75+
bar = '789';
76+
})(typeofAnnotation || (typeofAnnotation = {}));

code/declarationspaces/declarationspace.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,41 @@ namespace fourn {
2727

2828
namespace meh {
2929
var something = {};
30-
(function(something) {
31-
something.foo = 123;
32-
})(something || something = {})
30+
// (function(something) {
31+
// something.foo = 123;
32+
// })(something || something = {})
3333
}
3434

3535
namespace utility {
36-
function log(msg) {
36+
export function log(msg) {
3737
console.log(msg);
3838
}
39-
function error(msg) {
39+
export function error(msg) {
4040
console.error(msg);
4141
}
4242
}
4343

4444
// usage
4545
utility.log('Call me');
46-
utility.error('maybe!');
46+
utility.error('maybe!');
47+
48+
49+
module importing {
50+
class Foo { }
51+
var Bar = Foo;
52+
var bar: Bar; // ERROR: "cannot find name 'Bar'"
53+
}
54+
55+
namespace importing {
56+
export class Foo { }
57+
}
58+
59+
import Bar = importing.Foo;
60+
var bar: Bar; // Okay
61+
62+
namespace typeofAnnotation {
63+
var foo = 123;
64+
var bar: typeof foo; // `bar` has the same type as `foo` (here `number`)
65+
bar = 456; // Okay
66+
bar = '789'; // ERROR: Type `string` is not `assignable` to type `number`
67+
}

docs/project/declarationspaces.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,37 @@ var bar: foo; // ERROR: "cannot find name 'foo'"
4747
```
4848
The reason why it says `cannot find name` is because the name `foo` *is not defined* in the *type* declaration space.
4949

50+
### TIPS
51+
52+
#### Copying Stuff around in the Type Declaration Space
53+
54+
If you want to move a class around you might be tempted to do the following:
55+
56+
```ts
57+
class Foo { }
58+
var Bar = Foo;
59+
var bar: Bar; // ERROR: "cannot find name 'Bar'"
60+
```
61+
This is an error because `var` only copied the `Foo` into the *variable* declaration space and you therefore cannot use `Bar` as a type annotation. The proper way is to use the `import` keyword. Note that you can only use the `import` keyword in such a way if you are using *namespaces* or *modules* (more on these later):
62+
63+
```ts
64+
namespace importing {
65+
export class Foo { }
66+
}
67+
68+
import Bar = importing.Foo;
69+
var bar: Bar; // Okay
70+
```
71+
72+
#### Capturing the type of a variable
73+
74+
You can actually use a variable in a type annotation using the `typeof` operator. This allows you to tell the compiler that one variable is the same type as another. Here is an example to demonstrate this:
75+
76+
```ts
77+
var foo = 123;
78+
var bar: typeof foo; // `bar` has the same type as `foo` (here `number`)
79+
bar = 456; // Okay
80+
bar = '789'; // ERROR: Type `string` is not `assignable` to type `number`
81+
```
82+
5083
{% include "footer.md" %}

0 commit comments

Comments
 (0)