forked from basarat/typescript-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.ts
96 lines (82 loc) · 2.1 KB
/
functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
export namespace asdfasdfasdfasdflkjasdflkjasdflkjasdflkjasdf {
}
namespace parameter {
// variable annotation
var sampleVariable: { bar: number }
// function parameter
function foo(sampleParameter: { bar: number }) { }
}
namespace returnType {
interface Foo {
foo: string;
}
// Return type annotated as `: Foo`
function foo(sample: Foo) {
return sample;
}
}
namespace inferred {
interface Foo {
foo: string;
}
function foo(sample: Foo) {
return sample; // inferred return type 'Foo'
}
}
namespace misspelled {
function foo() {
return { fou: 'John Doe' }; // You might not find this misspelling `foo` till its too late
}
sendAsJSON(foo());
}
namespace optional {
function foo(bar: number, bas?: string): void {
// ..
}
foo(123);
foo(123, 'hello');
}
namespace optionalDefault {
function foo(bar: number, bas: string = 'world') {
console.log(bar, bas);
}
foo(123); // 123, world
foo(123, 'hello'); // 123, hello
}
namespace overloads {
export function padding(a: number, b?: number, c?: number, d?: any) {
if (b === undefined && c === undefined && d === undefined) {
b = c = d = a;
}
else if (c === undefined && d === undefined) {
c = a;
d = b;
}
return {
top: a,
right: b,
bottom: c,
left: d
};
}
}
namespace overloadsDone {
export function padding(all: number);
export function padding(topAndBottom: number, leftAndRight: number);
export function padding(top: number, right: number, bottom: number, left: number);
export function padding(a: number, b?: number, c?: number, d?: number) {
if (b === undefined && c === undefined && d === undefined) {
b = c = d = a;
}
else if (c === undefined && d === undefined) {
c = a;
d = b;
}
return {
top: a,
right: b,
bottom: c,
left: d
};
}
}