# Types code organization

**URL:** <https://discourse.dry-rb.org/t/types-code-organization/1808>\
**Category:** dry-types\
**Created:** [July 5, 2024, 2:00pm UTC](https://discourse.dry-rb.org/t/types-code-organization/1808 "2024-07-05T14:00:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elcuervo](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/elcuervo/32/900_2.png) [@elcuervo](https://discourse.dry-rb.org/u/elcuervo)\
**Post date:** [July 5, 2024, 2:00pm UTC](https://discourse.dry-rb.org/t/types-code-organization/1808/1 "2024-07-05T14:00:55Z")

</div>

I’m heavily pushing `dry-types` where I work because I feel that is providing a lot of value understanding the data structures we handle.  
Now I’ve been going back and forward on how to organize the types in a clean way trying to avoid duplication.

So far most of our types live in `lib/types.rb` but there are some specific implementations that usually just exist on the file using it

## Example

```ruby
module Types
  include Dry.Types()

  Type = Types.Instance(Dry::Types::Type)
  SquishedString = Types::Coercible::String >> :squish.to_proc
end

class SomeService
  Input = Types::Array.of(Types::SquishedString)

  def self.call(input)
    Input[input].map #...
  end
end

```

I can’t really think of a clean way to organize general types are more specific ones trying to prevent duplication.  
I thought of a `lib/types/#{specific_type}` kind of thing but I don’t love it. I just want to get a clean organization without having to jump many hoops.

What are your preferred techniques to organize types in a codebase?  
Thanks!

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [July 10, 2024, 7:46pm UTC](https://discourse.dry-rb.org/t/types-code-organization/1808/2 "2024-07-10T19:46:41Z")

</div>

Recommend you define your types module first, before opening it. This allows you to reopen nested modules.

```ruby
Types = Dry.Types()

module Types
  module Coercible
    SquishedString = String.constructor(&:squish)
  end
end

```

I use types extensively, and organize them exactly the way you describe. I think having to look in more places for them would only make it harder to understand.

---

<div class="post-metadata">

**Author:** ![elcuervo](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/elcuervo/32/900_2.png) [@elcuervo](https://discourse.dry-rb.org/u/elcuervo)\
**Post date:** [July 10, 2024, 7:59pm UTC](https://discourse.dry-rb.org/t/types-code-organization/1808/3 "2024-07-10T19:59:48Z")

</div>

Thanks for the tip!  
I’ve been doing ↓ but your version makes more sense

```ruby
module Types
  include Dry.Types()

  Coercible::Potato = Types::Symbol #...
end

```
