-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
``` |