# Defining types for struct fields

**URL:** <https://forum.elixirforum.com/t/defining-types-for-struct-fields/34929>\
**Category:** Questions\
**Tags:** beginners-questions\
**Created:** [October 14, 2020, 3:20pm UTC](https://forum.elixirforum.com/t/defining-types-for-struct-fields/34929 "2020-10-14T15:20:33Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [October 14, 2020, 3:46pm UTC](https://forum.elixirforum.com/t/defining-types-for-struct-fields/34929/2 "2020-10-14T15:46:43Z")

</div>

You can define a typespec for your struct but you cannot enforce it.

What you can do in your module is to use a constructor-like function:

```elixir
def new(props) do
   ... validate the values
   struct( __MODULE__ , props)
end

```

(`new` is not a special name)

---

_[View the full topic](https://forum.elixirforum.com/t/defining-types-for-struct-fields/34929)._
