Skip to content

Commit b73fefa

Browse files
committed
starting ambient docs
1 parent cbfccab commit b73fefa

File tree

6 files changed

+104
-2
lines changed

6 files changed

+104
-2
lines changed

SUMMARY.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
* [Namespaces](docs/project/namespaces.md)
2727
* [TypeScript's Type System](docs/types/type-system.md)
2828
* [JS Migration Guide](docs/types/migrating.md)
29-
* [Ambient Declarations](docs/types/ambient-declarations.md)
29+
* [Ambient Declarations](docs/types/ambient/intro.md)
30+
* [Declaration Files](docs/types/ambient/d.ts.md)
31+
* [Variables](docs/types/ambient/variables.md)
32+
* [Interfaces](docs/types/ambient/interfaces.md)
3033
* [`lib.d.ts`](docs/types/lib.d.ts.md)
3134
* [Functions](docs/types/type-compatability.md)
3235
* [Type Assertion](docs/types/type-assertion.md)

docs/types/ambient/d.ts.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### Declaration file
2+
You can tell TypeScript that you are trying to describe code that exists elsewhere (e.g. written in JavaScript/CoffeeScript/The runtime environment like the browser or nodejs) using the `declare` keyword. As a quick example:
3+
4+
```ts
5+
foo = 123; // Error: `foo` is not defined
6+
```
7+
vs.
8+
```ts
9+
declare var foo:any;
10+
foo = 123; // allowed
11+
```
12+
13+
You have the option of putting these declarations in a `.ts` file or in a `.d.ts` file. We highly recommend you in your real world projects you use a separate `.d.ts` (start with one called something like `globals.d.ts` or `vendor.d.ts`).
14+
15+
If a file has the extension `.d.ts` then each root level definition much have the `declare` keyword prefixed to it to make it clear that the author knows that there will be *no code emitted by TypeScript to ensure that this defined item will exist at runtime*.
16+
17+
> For ambient declarations : Runtime JavaScript equivalent is a promise that you are making with the compiler. If these do not exist at runtime and you try to you them, things will break without warning.

docs/types/ambient/interfaces.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
### Interfaces
2+
Interfaces have *zero* runtime JS impact. There is a lot of power in TypeScript interfaces to declare the structure of variables.
3+
4+
The following two are equivalent declarations, the first uses an *inline annotation*, the second uses an *interface*:
5+
6+
```ts
7+
// Sample A
8+
declare var myPoint: { x: number; y: number; };
9+
10+
// Sample B
11+
interface Point {
12+
x: number; y: number;
13+
}
14+
declare var myPoint: Point;
15+
```
16+
17+
However the beauty of *Sample B* is that if someone authors a library that builds on the `myPoint` library to add new members they can do that with if you used an interface:
18+
19+
```ts
20+
// Lib a.d.ts
21+
interface Point {
22+
x: number; y: number;
23+
}
24+
declare var myPoint: Point;
25+
26+
// Lib b.d.ts
27+
interface Point {
28+
x: number; y: number; z: number;
29+
}
30+
31+
// Your code
32+
var myPoint.z; // Allowed!
33+
```
34+
35+
This is because **interfaces in TypeScript are open ended**. This is a vital tenant of TypeScript that it allows you to mimic the extensibility of JavaScript using *interfaces*.

docs/types/ambient/intro.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Ambient Declarations
2+
3+
As we mentioned in [why TypeScript](../../why-typescript.md):
4+
5+
> A major design goal of TypeScript was to make it possible for you to safely and easily use existing JavaScript libraries in TypeScript. TypeScript does this by means of *declaration*
6+
7+
Ambient declarations allow you to *safely use existing popular JavaScript libraries* and *incrementally migrate your JavaScript/CoffeeScript/Others-Compile-To-Js-Language project to TypeScript*.
8+
9+
Studying patterns in ambient declarations for *third party JavaScript code* is good practice for annotating *your* TypeScript code base as well. This is why we present it so early on.
10+

docs/types/ambient/variables.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
### Variables
2+
For example to tell TypeScript about the [`process` variable](https://nodejs.org/api/process.html) you *can* do:
3+
4+
```ts
5+
declare var process:any;
6+
```
7+
8+
> You don't *need* to do this for `process` as there is already a [community maintained `node.ts`](https://github.com/borisyankov/DefinitelyTyped/blob/master/node/node.d.ts)
9+
10+
This allows you to use the `process` variable without TypeScript complaining:
11+
12+
```ts
13+
process.exit()
14+
```
15+
16+
We recommend using an interface wherever possible e.g:
17+
18+
```ts
19+
interface Process {
20+
exit(code?:number):void;
21+
}
22+
declare var process: Process;
23+
```
24+
25+
This allows other people to *extend* the nature of these global variables while still telling TypeScript about such modifications. E.g. consider the following case where we add an `exitWithLogging` function to process for our amusement:
26+
27+
```ts
28+
interface Process {
29+
exitWithLogging(code?:number):void;
30+
}
31+
process.exitWithLogging = function() {
32+
console.log("exiting");
33+
process.exit.apply(process,arguments);
34+
}
35+
```
36+
37+
Lets look at interfaces in a bit more detail next.

docs/types/migrating.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@ declare var $: JQuery;
7373

7474
This provides you an easier future update path.
7575

76-
Again, a high quality `jquery.d.ts` exists at [DefinitelyTyped](https://github.com/borisyankov/DefinitelyTyped). But you now know how to overcome any JavaScript -> TypeScript friction *quickly* when using third party JavaScript. We will look at ambient declarations in detail later.
76+
Again, a high quality `jquery.d.ts` exists at [DefinitelyTyped](https://github.com/borisyankov/DefinitelyTyped). But you now know how to overcome any JavaScript -> TypeScript friction *quickly* when using third party JavaScript. We will look at ambient declarations in detail next.

0 commit comments

Comments
 (0)