Skip to content

Commit 126e482

Browse files
committed
typescript annotations
1 parent e28fa11 commit 126e482

File tree

6 files changed

+388
-58
lines changed

6 files changed

+388
-58
lines changed

SUMMARY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
* [for...of](docs/for...of.md)
1212
* [Template Strings](docs/template-strings.md)
1313
* [Spread Operator](docs/spread-operator.md)
14+
* [TypeScript's Type System](docs/types/type-system.md)
15+
* [Ambient Declarations](docs/types/ambient-declarations.md)
16+
* [Functions](docs/types/type-compatability.md)
17+
* [Type Compatability](docs/types/type-compatability.md)

code/types/types.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ var null_undefined;
8888
foo: Null;
8989
bar: Undefined;
9090
})(null_undefined || (null_undefined = {}));
91+
var void_;
92+
(function (void_) {
93+
function log(message) {
94+
console.log(message);
95+
}
96+
})(void_ || (void_ = {}));
9197
var generics;
9298
(function (generics) {
9399
function reverse(items) {
@@ -133,3 +139,73 @@ var generics;
133139
'2'
134140
];
135141
})(generics || (generics = {}));
142+
var union;
143+
(function (union) {
144+
function formatCommandline(command) {
145+
var line = '';
146+
if (typeof command === 'string') {
147+
line = command.trim();
148+
}
149+
else {
150+
line = command.join(' ').trim();
151+
}
152+
}
153+
})(union || (union = {}));
154+
var tuple;
155+
(function (tuple) {
156+
var nameNumber;
157+
nameNumber = [
158+
'Jenny',
159+
8675309
160+
];
161+
nameNumber = [
162+
'Jenny',
163+
'867-5309'
164+
];
165+
var name = nameNumber[0], num = nameNumber[1];
166+
})(tuple || (tuple = {}));
167+
var getset;
168+
(function (getset) {
169+
var _value;
170+
function getOrSet(value) {
171+
if (value === undefined) {
172+
return _value;
173+
}
174+
else {
175+
_value = value;
176+
}
177+
}
178+
getOrSet(1);
179+
console.log(getOrSet());
180+
})(getset || (getset = {}));
181+
var getset_;
182+
(function (getset_) {
183+
var _value;
184+
function getOrSet(value) {
185+
if (value === undefined) {
186+
return _value;
187+
}
188+
else {
189+
_value = value;
190+
}
191+
}
192+
getOrSet(1);
193+
console.log(getOrSet());
194+
})(getset_ || (getset_ = {}));
195+
var overload;
196+
(function (overload) {
197+
function callMe(v1, v2) {
198+
}
199+
callMe();
200+
callMe(1);
201+
callMe('jenny', 5309);
202+
callMe('jenny');
203+
callMe('jenny', '5309');
204+
})(overload || (overload = {}));
205+
var alias;
206+
(function (alias) {
207+
var sample;
208+
sample = 123;
209+
sample = '123';
210+
sample = true;
211+
})(alias || (alias = {}));

code/types/types.ts

Lines changed: 107 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export module m1 {
1313
num = 123;
1414
num = 123.456;
1515
num = '123'; // Error
16-
16+
1717
str = '123';
1818
str = 123; // Error
1919

@@ -27,7 +27,7 @@ export module m2 {
2727

2828
boolArray = [true, false];
2929
console.log(boolArray[0]); // true
30-
console.log(boolArray.length); // 2
30+
console.log(boolArray.length); // 2
3131
boolArray[1] = true;
3232
boolArray = [false, false];
3333

@@ -78,7 +78,7 @@ export module m4 {
7878

7979
module any {
8080
var power: any;
81-
81+
8282
// Takes any and all types
8383
power = '123';
8484
power = 123;
@@ -92,16 +92,22 @@ module any {
9292
module null_undefined {
9393
var num: number;
9494
var str: string;
95-
95+
9696
// These literals can be assigned to anything
9797
num = null;
98-
str = undefined;
99-
98+
str = undefined;
99+
100100
// However they don't have a dedicated annotation
101101
foo: Null; // Error
102102
bar: Undefined; // Error
103103
}
104104

105+
module void_ {
106+
function log(message): void {
107+
console.log(message);
108+
}
109+
}
110+
105111
module generics {
106112
function reverse<T>(items: T[]): T[] {
107113
var toreturn = [];
@@ -114,25 +120,112 @@ module generics {
114120
var sample = [1, 2, 3];
115121
var reversed = reverse(sample);
116122
console.log(reversed); // 3,2,1
117-
123+
118124
// Safety!
119-
reversed[0] = '1'; // Error!
125+
reversed[0] = '1'; // Error!
120126
reversed = ['1', '2']; // Error!
121-
127+
122128
reversed[0] = 1; // Okay
123129
reversed = [1, 2]; // Okay
124-
130+
125131
//////////////////////
126-
132+
127133
var strArr = ['1', '2'];
128134
var reversedStrs = reverse(strArr);
129135

130136
reversedStr = [1, 2]; // Error!
131-
132-
///
137+
138+
///
133139
var numArr = [1, 2];
134140
var reversedNums = numArr.reverse();
135-
141+
136142
reversedNums = ['1', '2']; // Error!
137143
}
138144

145+
module union {
146+
function formatCommandline(command: string[]|string) {
147+
var line = '';
148+
if (typeof command === 'string') {
149+
line = command.trim();
150+
} else {
151+
line = command.join(' ').trim();
152+
}
153+
154+
// Do stuff with line:string
155+
}
156+
}
157+
158+
module tuple {
159+
var nameNumber: [string, number];
160+
161+
// Okay
162+
nameNumber = ['Jenny', 8675309];
163+
164+
// Error!
165+
nameNumber = ['Jenny', '867-5309'];
166+
167+
var [name, num] = nameNumber;
168+
}
169+
170+
module getset {
171+
172+
var _value;
173+
function getOrSet(value) {
174+
if (value === undefined) {
175+
return _value;
176+
} else {
177+
_value = value;
178+
}
179+
}
180+
181+
getOrSet(1); // set : 1
182+
console.log(getOrSet()); // get : 1
183+
}
184+
185+
module getset_ {
186+
187+
var _value;
188+
function getOrSet(): number;
189+
function getOrSet(value: number);
190+
function getOrSet(value?: number) {
191+
if (value === undefined) {
192+
return _value;
193+
} else {
194+
_value = value;
195+
}
196+
}
197+
198+
getOrSet(1); // set : 1
199+
console.log(getOrSet()); // get : 1
200+
}
201+
202+
203+
module overload {
204+
205+
function callMe(): number;
206+
function callMe(v1: number);
207+
function callMe(v1: string, v2: number);
208+
function callMe(v1?: any, v2?: any): any {
209+
// Implementation body goes here
210+
}
211+
212+
// Allowed calls
213+
callMe();
214+
callMe(1);
215+
callMe('jenny', 5309);
216+
217+
// ERROR: invalid calls:
218+
callMe('jenny');
219+
callMe('jenny', '5309');
220+
}
221+
222+
module alias {
223+
type StrOrNum = string|number;
224+
225+
var sample: StrOrNum;
226+
sample = 123;
227+
sample = '123';
228+
229+
// Safety
230+
sample = true; // Error!
231+
}

docs/types/advanced.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
2+
3+
4+
5+
# Functions
6+
7+
## Optional
8+
The `?` annotation can be used before a function argument or member of an interface to denote that a member is optional. That is to say that you can provide it if you want (and it will be type checked), but if it is ommited its *okay*. This is shown in the following example:
9+
10+
## Specialized Parameters
11+
12+
## Function Overloads
13+
The JavaScript runtime does not have runtime support for function overloading. There can be only a single function body for any given name in scope. However people do support function overloading by utilizing the highly dynamic nature of JavaScript e.g. a getter and a setter:
14+
15+
```ts
16+
var _value;
17+
function getOrSet(value) {
18+
if (value === undefined) {
19+
return _value;
20+
} else {
21+
_value = value;
22+
}
23+
}
24+
25+
getOrSet(1); // set : 1
26+
console.log(getOrSet()); // get : 1
27+
```
28+
29+
Such implementation can be captured by the TypeScript's type system by providing function signatures before the function implementation:
30+
31+
```ts
32+
var _value;
33+
function getOrSet(): number;
34+
function getOrSet(value: number);
35+
function getOrSet(value?: number) {
36+
if (value === undefined) {
37+
return _value;
38+
} else {
39+
_value = value;
40+
}
41+
}
42+
43+
getOrSet(1); // set : 1
44+
console.log(getOrSet()); // get : 1
45+
```
46+
47+
Note that when you define function overloads this way, *the last signature is actually not callable*. You have to provide it however to help the implementer of the function be aware of the consequences of his overload signatures. For example in the following example the function with the signature `function callMe(v1?: any, v2?: any): any` is not open to public use:
48+
49+
```ts
50+
function callMe(): number;
51+
function callMe(v1: number);
52+
function callMe(v1: string, v2: number);
53+
function callMe(v1?: any, v2?: any): any {
54+
// Implementation body goes here
55+
}
56+
57+
// Allowed calls
58+
callMe();
59+
callMe(1);
60+
callMe('jenny', 5309);
61+
62+
// COMPILER ERROR: invalid calls
63+
callMe('jenny');
64+
callMe('jenny', '5309');
65+
```
66+
67+
TIP: Note that there is a slight overlap between union types and function overloading. If two function signatures only differ by a single parameter having different types just use a union type for that parameter instead of creating an overload signature.
68+
69+
70+
# Interfaces
71+
72+
Interfaces have a lot of power in TypeScript. This is because they are designed to capture all the complexity of
73+
74+
75+
76+
77+
# Ambient Declarations
78+
79+
We previously had a brief look at ambient declarations in the section *why typescript?*. One of the core design goals of TypeScript is to allow easy consumption of existing JavaScript libraries. You can declare the type information for existing JavaScript using *ambient declarations*. You declare ambient stuff using the `declare` keyword. In fact this is how a bunch of stuff available by default in a browser environment (e.g `window`, `document` etc) is declared in a file called `lib.d.ts`
80+
81+
82+
Note: You can find type definitions for nearly 90% of the most popular JavaScript libraries at [DefinitelyTyped](https://github.com/borisyankov/DefinitelyTyped) with contributions from [lots of developers](https://github.com/borisyankov/DefinitelyTyped/graphs/contributors).
83+
84+
85+
86+
### lib.d.ts
87+
88+
# Interfaces
89+
90+
91+
92+
### Interfaces for primitive types
93+
94+
### Interface for array
95+
96+
## Type Alias
97+
98+
## Union Types
99+
needed for configuration objects
100+
101+
## Type Inference
102+
It tries to *infer* as much as it can *so that you don't need to explicitly type* your code.
103+
104+
## Function Signatures
105+
106+
Specialized
107+
108+
## Type Assertion
109+
110+
If A is a subtype of B or B is a subtype of A.
111+
112+
113+
114+
115+
116+
117+
118+
119+
[more on interfaces]
120+
Structural so more information is okay, but less information is an error. Duck typing is baked deep into the language design.
121+
Open Ended
122+
Type Compatibility

docs/types/functions.md

Whitespace-only changes.

0 commit comments

Comments
 (0)