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: docs/types/functions.md
+10-6Lines changed: 10 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,7 @@ var sampleVariable: { bar: number }
16
16
// function parameter
17
17
function foo(sampleParameter: { bar:number }) { }
18
18
```
19
+
19
20
Here I used inline type annotations. Of course you can use interfaces etc.
20
21
21
22
### Return type annotation
@@ -47,27 +48,28 @@ function foo(sample: Foo) {
47
48
}
48
49
```
49
50
50
-
However it is generally a good idea to add these annotation to help with errors e.g.
51
+
However it is generally a good idea to add these annotation to help with errors e.g.:
51
52
52
53
```ts
53
54
function foo() {
54
-
return { fou: 'John Doe' }; // You might not find this misspelling `foo` till its too late
55
+
return { fou: 'John Doe' }; // You might not find this misspelling `foo` till it's too late
55
56
}
56
57
57
58
sendAsJSON(foo());
58
59
```
60
+
59
61
If you don't plan to return anything from a function to you can annotate it as `:void`. You can generally drop `:void` and leave it to the inference engine though.
60
62
61
63
### Optional Parameters
62
-
You can mark a parameter as optional,
64
+
You can mark a parameter as optional:
63
65
64
66
```ts
65
67
function foo(bar:number, bas?:string):void {
66
68
// ..
67
69
}
68
70
69
71
foo(123);
70
-
foo(123,'hello');
72
+
foo(123,'hello');
71
73
```
72
74
73
75
Alternatively you can even provide a default value (using `= someValue` after the parameter declaration) which will get injected for you if the caller doesn't provide that argument.
If you look at the code carefully you realize the meaning of `a`,`b`,`c`,`d` change based on how many arguments are passed in. Also the function only expects `1`, `2` or `4` arguments. These constraints can be *enforced* and *documented* using function overloading. You just:
105
108
106
109
* declare the function header multiple times,
107
110
* the last function header is the one that is actually active *within* the function body but is not available to the outside world.
padding(1,1,1); // Error: Not a part of the available overloads
141
145
```
142
146
143
-
Of course its important for the final declaration (the true declaration as seen from inside the function) to be compatible with all the overloads. This is because that is the true nature of the function calls that the function body needs to account for.
147
+
Of course it's important for the final declaration (the true declaration as seen from inside the function) to be compatible with all the overloads. This is because that is the true nature of the function calls that the function body needs to account for.
144
148
145
149
> Function overloading in TypeScript doesn't come with any runtime overhead. It just allows you to document the manner you expect the function to be called in and the compiler holds the rest of your code in check.
0 commit comments