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