Skip to content

Commit

Permalink
docs: add docs for guards
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Streicher authored and coronoro committed Nov 30, 2023
1 parent 77d8a10 commit 3b7ed8c
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default defineConfig({
text: 'Advanced',
items: [
{text: 'Typescript', link: '/advanced-typescript'},
{text: 'Type Guards', link: '/advanced-typescript-guards'},
{text: 'convertFilterSetConfig', link: '/convert-filter-set-config'},
],
},
Expand Down
98 changes: 98 additions & 0 deletions docs/advanced-typescript-guards.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Type Guards

Because the `FilterSetConfig` type is complex contains multiple possible subtypes it can be hard to access a specific attribute of the config.
To tackle this problem we offer type guards.


## isFilterSetValue
This type guard checks if passed parameter is a `FilterSetValue`:
``` ts
const config: FilterSetConfig<Category> = {
id: {
value: 1
}
}
...
if (isFilterSetValue(config.id)){
config.id.value = 2
}
```
::: tip
This type guard checks for undefined. So if the config contains `{value:undefined}` it will not be seen as a `FilterSetValue`
:::

## isFilterSetRange
This type guard checks if passed parameter is a `FilterSetRange`:
``` ts
const config: FilterSetConfig<Category> = {
id: {
gt: 10
}
}
...
if (isFilterSetRange(config.id)){
config.id.gt = 3
}
```


## isFilterSetExact
This type guard checks if passed parameter is a `FilterSetExact`:
``` ts
const config: FilterSetConfig<Category> = {
id: {
exact: 1
}
}
...
if (isFilterSetExact(config.id)){
config.id.exact = 2
}
```

## isFilterSetIn
This type guard checks if passed parameter is a `FilterSetIn`:
``` ts
const config: FilterSetConfig<Category> = {
id: {
in: [1]
}
}
...
if (isFilterSetIn(config.id)){
config.id.in = [2,3]
}
```

## isFilterSetAffix
This type guard checks if passed parameter is a `FilterSetAffix`:
``` ts
const config: FilterSetConfig<Category> = {
id: {
startswith: 1
}
}
...
if (isFilterSetAffix(config.id)){
config.id.startswith = 2
}
```


## isFilterSetConfig
This type guard checks if passed parameter is a `FilterSetConfig`:
This can be used for nested complex types in combination with the other type guards.

``` ts
const config: FilterSetConfig<Book> = {
category: {
id: {
value: 1
}
}
}
...
if (isFilterSetConfig(config.category) && isFilterSetValue(config.category.id) ){
config.category.id.value = 2
}
```
98 changes: 98 additions & 0 deletions docs/de/advanced-typescript-guards.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Type Guards

Weil der `FilterSetConfig` Typ sich aus mehreren Subtypen zusammensetzt ist es nicht einfach auf die Attribute zuzugreifen.
Aus diesem Grund stellt die Bibliothek Type-Guards bereit.


## isFilterSetValue
Dieser Type-Guard überprüft ob der übergebene Paramneter vom Typ `FilterSetValue`ist:
``` ts
const config: FilterSetConfig<Category> = {
id: {
value: 1
}
}
...
if (isFilterSetValue(config.id)){
config.id.value = 2
}
```
::: tip
Dieser Type-Guard überprüft ob value nicht undefined ist. Wenn die Konfiguration `{value:undefined}` enthält wird diese nicht `FilterSetValue` erkannt.
:::

## isFilterSetRange
Dieser Type-Guard überprüft ob der übergebene Paramneter vom Typ `FilterSetRange`ist:
``` ts
const config: FilterSetConfig<Category> = {
id: {
gt: 10
}
}
...
if (isFilterSetRange(config.id)){
config.id.gt = 3
}
```


## isFilterSetExact
Dieser Type-Guard überprüft ob der übergebene Paramneter vom Typ `FilterSetExact`ist:
``` ts
const config: FilterSetConfig<Category> = {
id: {
exact: 1
}
}
...
if (isFilterSetExact(config.id)){
config.id.exact = 2
}
```

## isFilterSetIn
Dieser Type-Guard überprüft ob der übergebene Paramneter vom Typ `FilterSetIn`ist:
``` ts
const config: FilterSetConfig<Category> = {
id: {
in: [1]
}
}
...
if (isFilterSetIn(config.id)){
config.id.in = [2,3]
}
```

## isFilterSetAffix
Dieser Type-Guard überprüft ob der übergebene Paramneter vom Typ `FilterSetAffix`ist:
``` ts
const config: FilterSetConfig<Category> = {
id: {
startswith: 1
}
}
...
if (isFilterSetAffix(config.id)){
config.id.startswith = 2
}
```


## isFilterSetConfig
Dieser Type-Guard überprüft ob der übergebene Paramneter vom Typ `FilterSetConfig`ist:
Dieser kann in kombinaion mit anderen Guards genutzt werden für Komplexe Type,

``` ts
const config: FilterSetConfig<Book> = {
category: {
id: {
value: 1
}
}
}
...
if (isFilterSetConfig(config.category) && isFilterSetValue(config.category.id) ){
config.category.id.value = 2
}
```

0 comments on commit 3b7ed8c

Please sign in to comment.