Skip to content

Commit df4080c

Browse files
committed
Added fixes for functions.md
1 parent 0ba6a42 commit df4080c

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

docs/types/functions.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var sampleVariable: { bar: number }
1616
// function parameter
1717
function foo(sampleParameter: { bar: number }) { }
1818
```
19+
1920
Here I used inline type annotations. Of course you can use interfaces etc.
2021

2122
### Return type annotation
@@ -47,27 +48,28 @@ function foo(sample: Foo) {
4748
}
4849
```
4950

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.:
5152

5253
```ts
5354
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
5556
}
5657

5758
sendAsJSON(foo());
5859
```
60+
5961
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.
6062

6163
### Optional Parameters
62-
You can mark a parameter as optional,
64+
You can mark a parameter as optional:
6365

6466
```ts
6567
function foo(bar: number, bas?: string): void {
6668
// ..
6769
}
6870

6971
foo(123);
70-
foo(123,'hello');
72+
foo(123, 'hello');
7173
```
7274

7375
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.
@@ -101,12 +103,14 @@ function padding(a: number, b?: number, c?: number, d?: any) {
101103
};
102104
}
103105
```
106+
104107
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:
105108

106109
* declare the function header multiple times,
107110
* the last function header is the one that is actually active *within* the function body but is not available to the outside world.
108111

109112
This is shown below:
113+
110114
```ts
111115
// Overloads
112116
function padding(all: number);
@@ -130,7 +134,7 @@ function padding(a: number, b?: number, c?: number, d?: number) {
130134
}
131135
```
132136

133-
Here the first three function signatures are what a available as valid calls to `padding`:
137+
Here the first three function signatures are what is available as valid calls to `padding`:
134138

135139
```ts
136140
padding(1); // Okay : all
@@ -140,7 +144,7 @@ padding(1,1,1,1); // Okay : top, right, bottom, left
140144
padding(1,1,1); // Error: Not a part of the available overloads
141145
```
142146

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

145149
> 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.
146150

0 commit comments

Comments
 (0)