# How to get the type of a class variable?

**URL:** <https://forum.crystal-lang.org/t/how-to-get-the-type-of-a-class-variable/5116>\
**Category:** Help & Support\
**Created:** [November 17, 2022, 6:04pm UTC](https://forum.crystal-lang.org/t/how-to-get-the-type-of-a-class-variable/5116 "2022-11-17T18:04:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![notramo](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@notramo](https://forum.crystal-lang.org/u/notramo)\
**Post date:** [November 17, 2022, 6:04pm UTC](https://forum.crystal-lang.org/t/how-to-get-the-type-of-a-class-variable/5116/1 "2022-11-17T18:04:13Z")

</div>

I am looking for a way to get the type of a class variable with macros, but I couldn’t find the appropriate macro methods.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [November 17, 2022, 7:54pm UTC](https://forum.crystal-lang.org/t/how-to-get-the-type-of-a-class-variable/5116/2 "2022-11-17T19:54:37Z")

</div>

```crystal
class Foo
  class_property foo : String = ""
  
  def self.foo_type
    {{ @type.class_vars.find{|var| var.name == "foo"}.type }}
  end
end
 
Foo.foo_type # => String

```

---

<div class="post-metadata">

**Author:** ![notramo](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@notramo](https://forum.crystal-lang.org/u/notramo)\
**Post date:** [November 17, 2022, 8:08pm UTC](https://forum.crystal-lang.org/t/how-to-get-the-type-of-a-class-variable/5116/3 "2022-11-17T20:08:12Z")

</div>

Thanks.
