Map typespec question

typespecs

#1

In https://hexdocs.pm/elixir/typespecs.html

There’s a definition for map literals:

  | %{}                                   # empty map
  | %{key: value_type}                    # map with required key :key of value_type
  | %{required(key_type) => value_type}   # map with required pairs of key_type and value_type
  | %{optional(key_type) => value_type}   # map with optional pairs of key_type and value_type
  | %SomeStruct{}                         # struct with all fields of any type
  | %SomeStruct{key: value_type}          # struct with required key :key of value_type

Does anyone know if

%{key: value_type}

implies the presence of optional(any) => any in addition to the required key?


#2

It does not.


#3

thanks! Just to be clear, this syntax is slightly inconsistent with the Struct, which does (ofc) imply the presence of keys “not in” the typespec literal


#4

It does not. It behaves in exactly the same way the “normal” map typespec works.