Dialyzer Unknown type for struct

Background

I have a custom struct and I want to define a type for it so I can run Dialyzer checks. However Dialyzer is complaining that I have an unknown type.

Code

I have a struct that defines an Event and I have defined the following type:

defmodule MyApp.Event do

  @enforce_keys [:event_id, :type, :version]
  defstruct event_id: nil,
            type: "event",
            version: nil

  @type t() :: %__MODULE__{
          event_id: non_neg_integer,
          type: String.t(),
          version: non_neg_integer
        }

  @spec new(map) :: __MODULE__.t()
  def new(map),
    do: %__MODULE__{
      event_id: Map.get(map, "event_id"),
      type: Map.get(map, "type"),
      version: Map.get(map, "v")
    }
end

Problem

However when I run mix dialyzer I get the following error:

:0: Unknown type ‘Elixir.Event’:t/0

What am I missing?

Somewhere in your code you are refering to Event.t but you have not an alias MyApp.Event there.

2 Likes

Also if you want to cut down on the boilerplate for the exact same result you can use typed_struct. I don’t usually like to add tons of “helper” deps like this but in this case it’s so natural I use it all the time.

1 Like

@NobbZ If only Dialyzer told me which file has the issue, it would be a start :stuck_out_tongue: I found it now, thanks!

@tme_317 I am a fan of the library and I use it in my projects.
However, I decided to not bring that yet to work. People may feel overloaded.

Besides, I find it better to use vanilla examples in the forum, this way the number of people able to help me is bigger :smiley:

1 Like