You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/Writing Definition Files.md
+24-10Lines changed: 24 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,32 @@
1
1
# Introduction
2
-
When using an external JavaScript library, or new host API, you'll need to use a declaration file (.d.ts) to describe the shape of that library. This guide covers a few high-level concepts specific to writing definition files, then proceeds with a number of examples that show how to transcribe various concepts to their matching definition file descriptions.
2
+
When using an external JavaScript library, or new host API, you'll need to use a declaration file (.d.ts) to describe the shape of that library.
3
+
This guide covers a few high-level concepts specific to writing definition files, then proceeds with a number of examples that show how to transcribe various concepts to their matching definition file descriptions.
3
4
4
5
# Guidelines and Specifics
5
6
6
7
## Workflow
7
8
8
-
The best way to write a .d.ts file is to start from the documentation of the library, not the code. Working from the documentation ensures the surface you present isn't muddied with implementation details, and is typically much easier to read than JS code. The examples below will be written as if you were reading documentation that presented example calling code.
9
+
The best way to write a .d.ts file is to start from the documentation of the library, not the code.
10
+
Working from the documentation ensures the surface you present isn't muddied with implementation details, and is typically much easier to read than JS code.
11
+
The examples below will be written as if you were reading documentation that presented example calling code.
9
12
10
13
## Namespacing
11
14
12
-
When defining interfaces (for example, "options" objects), you have a choice about whether to put these types inside a module or not. This is largely a judgement call -- if the consumer is likely to often declare variables or parameters of that type, and the type can be named without risk of colliding with other types, prefer placing it in the global namespace. If the type is not likely to be referenced directly, or can't be named with a reasonably unique name, do use a module to prevent it from colliding with other types.
15
+
When defining interfaces (for example, "options" objects), you have a choice about whether to put these types inside a module or not.
16
+
This is largely a judgement call -- if the consumer is likely to often declare variables or parameters of that type, and the type can be named without risk of colliding with other types, prefer placing it in the global namespace.
17
+
If the type is not likely to be referenced directly, or can't be named with a reasonably unique name, do use a module to prevent it from colliding with other types.
13
18
14
19
## Callbacks
15
20
16
-
Many JavaScript libraries take a function as a parameter, then invoke that function later with a known set of arguments. When writing the function signatures for these types, *do not* mark those parameters as optional. The right way to think of this is *"What parameters will be provided?"*, not *"What parameters will be consumed?"*. While TypeScript 0.9.7 and above does not enforce that the optionality, bivariance on argument optionality might be enforced by an external linter.
21
+
Many JavaScript libraries take a function as a parameter, then invoke that function later with a known set of arguments.
22
+
When writing the function signatures for these types, *do not* mark those parameters as optional.
23
+
The right way to think of this is *"What parameters will be provided?"*, not *"What parameters will be consumed?"*.
24
+
While TypeScript 0.9.7 and above does not enforce that the optionality, bivariance on argument optionality might be enforced by an external linter.
17
25
18
26
## Extensibility and Declaration Merging
19
27
20
-
When writing definition files, it's important to remember TypeScript's rules for extending existing objects. You might have a choice of declaring a variable using an anonymous type or an interface type:
28
+
When writing definition files, it's important to remember TypeScript's rules for extending existing objects.
29
+
You might have a choice of declaring a variable using an anonymous type or an interface type:
Whether or not you want your declarations to be extensible in this way is a bit of a judgement call. As always, try to represent the intent of the library here.
51
+
Whether or not you want your declarations to be extensible in this way is a bit of a judgement call.
52
+
As always, try to represent the intent of the library here.
43
53
44
54
## Class Decomposition
45
55
46
-
Classes in TypeScript create two separate types: the instance type, which defines what members an instance of a class has, and the constructor function type, which defines what members the class constructor function has. The constructor function type is also known as the "static side" type because it includes static members of the class.
56
+
Classes in TypeScript create two separate types: the instance type, which defines what members an instance of a class has, and the constructor function type, which defines what members the class constructor function has.
57
+
The constructor function type is also known as the "static side" type because it includes static members of the class.
47
58
48
59
While you can reference the static side of a class using the `typeof` keyword, it is sometimes useful or necessary when writing definition files to use the *decomposed class* pattern which explicitly separates the instance and static types of class.
49
60
@@ -80,11 +91,13 @@ The trade-offs here are as follows:
80
91
81
92
## Naming Conventions
82
93
83
-
In general, do not prefix interfaces with `I` (e.g. `IColor`). Because the concept of an interface in TypeScript is much more broad than in C# or Java, the `IFoo` naming convention is not broadly useful.
94
+
In general, do not prefix interfaces with `I` (e.g. `IColor`).
95
+
Because the concept of an interface in TypeScript is much more broad than in C# or Java, the `IFoo` naming convention is not broadly useful.
84
96
85
97
# Examples
86
98
87
-
Let's jump in to the examples section. For each example, sample *usage* of the library is provided, followed by the definition code that accurately types the usage. When there are multiple good representations, more than one definition sample might be listed.
99
+
Let's jump in to the examples section. For each example, sample *usage* of the library is provided, followed by the definition code that accurately types the usage.
100
+
When there are multiple good representations, more than one definition sample might be listed.
function addLater(x:number, y:number, (sum:number) =>void):void;
228
241
```
229
242
230
-
Please post a comment [here|https://github.com/Microsoft/TypeScript/issues] if there's a pattern you'd like to see documented! We'll add to this as we can.
243
+
Please post a comment [here](https://github.com/Microsoft/TypeScript/issues) if there's a pattern you'd like to see documented!
0 commit comments