Skip to content

Commit 184a630

Browse files
committed
New: added glossary entry Type Guard
1 parent af116f8 commit 184a630

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ The following changes have been completed, and will be included in the next tagg
105105
- added Type Alias
106106
- added Type Casting
107107
- added Type Guarantee
108+
- added Type Guard
108109
- added Value Object
109110

110111
### Fixes

TEMPLATE.md

+1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,5 @@ Table of Contents:
6565
[Type Alias]: ../../glossary/type-alias.md
6666
[Type Casting]: ../../glossary/type-casting.md
6767
[Type Guarantee]: ../../glossary/type-guarantee.md
68+
[Type Guard]: ../../glossary/type-guard.md
6869
[Value Object]: ../../glossary/value-object.md

glossary/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Table of Contents:
5151
- [Type Alias][Type Alias]
5252
- [Type Casting][Type Casting]
5353
- [Type Guarantee][Type Guarantee]
54+
- [Type Guard][Type Guard]
5455

5556
[ADOPTION]: ../impacted-areas/ADOPTION.md
5657
[CONTRIBUTIONS]: ../impacted-areas/CONTRIBUTIONS.md

glossary/type-guard.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Type Guard
2+
3+
A _type guard_ is a function that returns a [type predicate][Type Predicate]. _Type guards_ are a type of [data guard][Data Guard].
4+
5+
We use them in Typescript code to tell the compiler which operations are safe to perform on any piece of data of indeterminate type.
6+
7+
```typescript
8+
function isValue<T = any>(
9+
input: unknown
10+
): input is Value<T> {
11+
return ((input as Value).implementsValue
12+
&& (input as Value).implementsValue());
13+
}
14+
```
15+
16+
End-users rely on correctly-functioning _type guards_ to avoid using Typescript's `as XXX` [type casting][Type Casting] in their code.
17+
18+
It's almost impossible to write a type-safe library or application without creating _type guards_.
19+
20+
[ADOPTION]: ../impacted-areas/ADOPTION.md
21+
[CONTRIBUTIONS]: ../impacted-areas/CONTRIBUTIONS.md
22+
[CORRECTNESS]: ../impacted-areas/CORRECTNESS.md
23+
[GOVERNANCE]: ../impacted-areas/GOVERNANCE.md
24+
[PROJECT-MAINTENANCE]: ../impacted-areas/PROJECT-MAINTENANCE.md
25+
[ROBUSTNESS]: ../impacted-areas/ROBUSTNESS.md
26+
[SECURITY]: ../impacted-areas/SECURITY.md
27+
[TESTABILITY]: ../impacted-areas/TESTABILITY.md
28+
[Base Class]: ./base-class.md
29+
[Branded Type]: ./branded-type.md
30+
[Caller]: ./caller.md
31+
[CQRS]: ./CQRS.md
32+
[Data Bag]: ./data-bag.md
33+
[Data Guard]: ./data-guard.md
34+
[Data Guarantee]: ./data-guarantee.md
35+
[Default Value]: ./default-value.md
36+
[Defensive Programming]: ./defensive-programming.md
37+
[Dependency]: ./dependency.md
38+
[Dependency Injection]: ./dependency-injection.md
39+
[Docblock]: ./docblock.md
40+
[End-User]: ./end-user.md
41+
[Entity]: ./entity.md
42+
[Exported Item]: ./exported-item.md
43+
[Flavoured Type]: ./flavoured-type.md
44+
[Function Prefix]: ./function-prefix.md
45+
[Function Signature]: ./function-signature.md
46+
[Hard-Coded]: ./hard-coded.md
47+
[Identity]: ./identity.md
48+
[Immutability]: ./immutability.md
49+
[Inherited Method]: ./inherited-method.md
50+
[Instantiable Type]: ./instantiable-type.md
51+
[Mandatory Dependency]: ./mandatory-dependency.md
52+
[Nominal Typing]: ./nominal-typing.md
53+
[Optional Input]: ./optional-input.md
54+
[Overridden Method]: ./overridden-method.md
55+
[Plain Object]: ./plain-object.md
56+
[Primitive Type]: ./primitive-type.md
57+
[Protocol]: ./protocol.md
58+
[Refined Type]: ./refined-type.md
59+
[Rest Parameter]: ./rest-parameter.md
60+
[Reusability]: ./reusability.md
61+
[Side Effects]: ./side-effects.md
62+
[Smart Constructor]: ./smart-constructor.md
63+
[Structural Typing]: ./structural-typing.md
64+
[Type Alias]: ./type-alias.md
65+
[Type Casting]: ./type-casting.md
66+
[Type Guarantee]: ./type-guarantee.md
67+
[Type Guard]: ./type-guard.md
68+
[Type Inference]: ./type-inference.md
69+
[Type Predicate]: ./type-predicate.md
70+
[Type Signature]: ./type-signature.md
71+
[User-Supplied Functional Options]: ./user-supplied-functional-options.md
72+
[User-Supplied Input]: ./user-supplied-input.md
73+
[User-Supplied Options]: ./user-supplied-options.md
74+
[User-Supplied Optional Dependencies]: ./user-supplied-optional-dependencies.md
75+
[Value]: ./value.md
76+
[Value Object]: ./value-object.md

0 commit comments

Comments
 (0)