Skip to content

Commit 50257f7

Browse files
Put sentences on different lines in 'Writing Definition Files'
1 parent b9fea6a commit 50257f7

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

pages/Writing Definition Files.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
# 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.
34

45
# Guidelines and Specifics
56

67
## Workflow
78

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.
912

1013
## Namespacing
1114

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.
1318

1419
## Callbacks
1520

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.
1725

1826
## Extensibility and Declaration Merging
1927

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:
2130

2231
#### Anonymously-typed var
2332

@@ -39,11 +48,13 @@ interface SomePoint { z: number; }
3948
MyPoint.z = 4; // OK
4049
```
4150

42-
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.
4353

4454
## Class Decomposition
4555

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.
4758

4859
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.
4960

@@ -80,11 +91,13 @@ The trade-offs here are as follows:
8091

8192
## Naming Conventions
8293

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.
8496

8597
# Examples
8698

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.
88101

89102
## Options Objects
90103

@@ -227,4 +240,5 @@ addLater(3, 4, (x) => console.log('x = ' + x));
227240
function addLater(x: number, y: number, (sum: number) => void): void;
228241
```
229242

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!
244+
We'll add to this as we can.

0 commit comments

Comments
 (0)