#Correctly typing Phoenix component attribute

1 messages · Page 1 of 1 (latest)

visual quarry
#

I'm trying to pass as an attribute a key to a map, which could be an atom or a string. However, I cannot get the attribute definition to work so that it won't warn me when passing in the second type listed.

Source:

attr :src, :map, required: true
attr :key, :string | :atom, required: true

# usage 
<.dx_label src={@profile} key={:surname} />

Thoughts?

visual quarry
#

using :any is the closest I can get... but it seems like there should be a better solution here

scarlet current
#

unfortunately there isn't, the phoenix component attributes typing is very primitive right now. It might get better in the future, but for now the best thing to do is just use :any and provide docs that explain further:

attr :key, :any, required: true, docs: "string or atom"

although technically really anything can be a key in a map.

#

the component type system doesn't really do much anyway, so using :any isn't a big problem. I'd say it's mostly about documentation. It does give you a compile-time warning if you're using a literal value of the wrong type, but it can't detect non-literal values and anything at runtime anyway, so it's not like the type annotations are actually helping that much