# How to typespec guards in a human friendly way?

**URL:** <https://forum.devtalk.com/t/how-to-typespec-guards-in-a-human-friendly-way/93661>\
**Category:** Backend Questions/Help\
**Tags:** dialyzer, elixir, types\
**Created:** [26 January 2023 10:07 UTC](https://forum.devtalk.com/t/how-to-typespec-guards-in-a-human-friendly-way/93661 "2023-01-26T10:07:22Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Fl4m3Ph03n1x](https://forum.devtalk.com/user_avatar/forum.devtalk.com/fl4m3ph03n1x/32/3808_2.png) [@Fl4m3Ph03n1x](https://forum.devtalk.com/u/Fl4m3Ph03n1x)\
**Post date:** [10 February 2023 13:29 UTC](https://forum.devtalk.com/t/how-to-typespec-guards-in-a-human-friendly-way/93661/3 "2023-02-10T13:29:04Z")

</div>

## Solution

What’s happening here is that `defguard` receives code and returns code (to be more specific, it both receives a AST representation of code and then returns that AST modified). As as consequence, this means that the correct typespec would be this:

```auto
@spec is_pos_integer(Macro.t()) :: Macro.t()

```

Which many people in the community defend is not very useful.  
I agree with this consensus, so the next best option is to add a `@doc` to the guard to clearly document it, and is what I suggest as well.

Sources:

- [https://stackoverflow.com/a/75307276/1337392](https://stackoverflow.com/a/75307276/1337392)
- [How to typespec guards in a human friendly way? - #3 by kip - Questions / Help - Elixir Programming Language Forum](https://elixirforum.com/t/how-to-typespec-guards-in-a-human-friendly-way/53437/3?u=fl4m3ph03n1x)

---

_[View the full topic](https://forum.devtalk.com/t/how-to-typespec-guards-in-a-human-friendly-way/93661)._
