Skip to content

Commit 3b7ed8c

Browse files
Tim Streichercoronoro
authored andcommitted
docs: add docs for guards
1 parent 77d8a10 commit 3b7ed8c

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default defineConfig({
3434
text: 'Advanced',
3535
items: [
3636
{text: 'Typescript', link: '/advanced-typescript'},
37+
{text: 'Type Guards', link: '/advanced-typescript-guards'},
3738
{text: 'convertFilterSetConfig', link: '/convert-filter-set-config'},
3839
],
3940
},

docs/advanced-typescript-guards.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Type Guards
2+
3+
Because the `FilterSetConfig` type is complex contains multiple possible subtypes it can be hard to access a specific attribute of the config.
4+
To tackle this problem we offer type guards.
5+
6+
7+
## isFilterSetValue
8+
This type guard checks if passed parameter is a `FilterSetValue`:
9+
``` ts
10+
const config: FilterSetConfig<Category> = {
11+
id: {
12+
value: 1
13+
}
14+
}
15+
...
16+
if (isFilterSetValue(config.id)){
17+
config.id.value = 2
18+
}
19+
```
20+
::: tip
21+
This type guard checks for undefined. So if the config contains `{value:undefined}` it will not be seen as a `FilterSetValue`
22+
:::
23+
24+
## isFilterSetRange
25+
This type guard checks if passed parameter is a `FilterSetRange`:
26+
``` ts
27+
const config: FilterSetConfig<Category> = {
28+
id: {
29+
gt: 10
30+
}
31+
}
32+
...
33+
if (isFilterSetRange(config.id)){
34+
config.id.gt = 3
35+
}
36+
```
37+
38+
39+
## isFilterSetExact
40+
This type guard checks if passed parameter is a `FilterSetExact`:
41+
``` ts
42+
const config: FilterSetConfig<Category> = {
43+
id: {
44+
exact: 1
45+
}
46+
}
47+
...
48+
if (isFilterSetExact(config.id)){
49+
config.id.exact = 2
50+
}
51+
```
52+
53+
## isFilterSetIn
54+
This type guard checks if passed parameter is a `FilterSetIn`:
55+
``` ts
56+
const config: FilterSetConfig<Category> = {
57+
id: {
58+
in: [1]
59+
}
60+
}
61+
...
62+
if (isFilterSetIn(config.id)){
63+
config.id.in = [2,3]
64+
}
65+
```
66+
67+
## isFilterSetAffix
68+
This type guard checks if passed parameter is a `FilterSetAffix`:
69+
``` ts
70+
const config: FilterSetConfig<Category> = {
71+
id: {
72+
startswith: 1
73+
}
74+
}
75+
...
76+
if (isFilterSetAffix(config.id)){
77+
config.id.startswith = 2
78+
}
79+
```
80+
81+
82+
## isFilterSetConfig
83+
This type guard checks if passed parameter is a `FilterSetConfig`:
84+
This can be used for nested complex types in combination with the other type guards.
85+
86+
``` ts
87+
const config: FilterSetConfig<Book> = {
88+
category: {
89+
id: {
90+
value: 1
91+
}
92+
}
93+
}
94+
...
95+
if (isFilterSetConfig(config.category) && isFilterSetValue(config.category.id) ){
96+
config.category.id.value = 2
97+
}
98+
```
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Type Guards
2+
3+
Weil der `FilterSetConfig` Typ sich aus mehreren Subtypen zusammensetzt ist es nicht einfach auf die Attribute zuzugreifen.
4+
Aus diesem Grund stellt die Bibliothek Type-Guards bereit.
5+
6+
7+
## isFilterSetValue
8+
Dieser Type-Guard überprüft ob der übergebene Paramneter vom Typ `FilterSetValue`ist:
9+
``` ts
10+
const config: FilterSetConfig<Category> = {
11+
id: {
12+
value: 1
13+
}
14+
}
15+
...
16+
if (isFilterSetValue(config.id)){
17+
config.id.value = 2
18+
}
19+
```
20+
::: tip
21+
Dieser Type-Guard überprüft ob value nicht undefined ist. Wenn die Konfiguration `{value:undefined}` enthält wird diese nicht `FilterSetValue` erkannt.
22+
:::
23+
24+
## isFilterSetRange
25+
Dieser Type-Guard überprüft ob der übergebene Paramneter vom Typ `FilterSetRange`ist:
26+
``` ts
27+
const config: FilterSetConfig<Category> = {
28+
id: {
29+
gt: 10
30+
}
31+
}
32+
...
33+
if (isFilterSetRange(config.id)){
34+
config.id.gt = 3
35+
}
36+
```
37+
38+
39+
## isFilterSetExact
40+
Dieser Type-Guard überprüft ob der übergebene Paramneter vom Typ `FilterSetExact`ist:
41+
``` ts
42+
const config: FilterSetConfig<Category> = {
43+
id: {
44+
exact: 1
45+
}
46+
}
47+
...
48+
if (isFilterSetExact(config.id)){
49+
config.id.exact = 2
50+
}
51+
```
52+
53+
## isFilterSetIn
54+
Dieser Type-Guard überprüft ob der übergebene Paramneter vom Typ `FilterSetIn`ist:
55+
``` ts
56+
const config: FilterSetConfig<Category> = {
57+
id: {
58+
in: [1]
59+
}
60+
}
61+
...
62+
if (isFilterSetIn(config.id)){
63+
config.id.in = [2,3]
64+
}
65+
```
66+
67+
## isFilterSetAffix
68+
Dieser Type-Guard überprüft ob der übergebene Paramneter vom Typ `FilterSetAffix`ist:
69+
``` ts
70+
const config: FilterSetConfig<Category> = {
71+
id: {
72+
startswith: 1
73+
}
74+
}
75+
...
76+
if (isFilterSetAffix(config.id)){
77+
config.id.startswith = 2
78+
}
79+
```
80+
81+
82+
## isFilterSetConfig
83+
Dieser Type-Guard überprüft ob der übergebene Paramneter vom Typ `FilterSetConfig`ist:
84+
Dieser kann in kombinaion mit anderen Guards genutzt werden für Komplexe Type,
85+
86+
``` ts
87+
const config: FilterSetConfig<Book> = {
88+
category: {
89+
id: {
90+
value: 1
91+
}
92+
}
93+
}
94+
...
95+
if (isFilterSetConfig(config.category) && isFilterSetValue(config.category.id) ){
96+
config.category.id.value = 2
97+
}
98+
```

0 commit comments

Comments
 (0)