···3737 NilableProcableType = NilableType.new(ProcableType).freeze
38383939 # Matches any value except `nil`. Use `_Any?` or `_Void` to match any value including `nil`.
4040+ # ```ruby
4141+ # _Any
4242+ # ```
4043 def _Any
4144 AnyType::Instance
4245 end
43464747+ # Matches any value including `nil`. This is the same as `_Void` and the opposite of `_Never`.
4848+ # ```ruby
4949+ # _Any?
5050+ # ```
4451 def _Any?
4552 VoidType::Instance
4653 end
47544848- # Matches if the value is an `Array` and all the elements match the given type.
4949- def _Array(...)
5050- ArrayType.new(...)
5555+ # Matches if the value is an `Array` and all the elements of the array match the given type.
5656+ # ```ruby
5757+ # _Array(String)
5858+ # ```
5959+ def _Array(type)
6060+ ArrayType.new(type)
5161 end
52625353- # Nilable version of `_Array`
5454- def _Array?(...)
6363+ # Nilable version of `_Array`.
6464+ # ```ruby
6565+ # _Array?(String)
6666+ # ```
6767+ def _Array?(type)
5568 NilableType.new(
5656- ArrayType.new(...)
6969+ ArrayType.new(type)
5770 )
5871 end
59726060- # Matches if the value is `true` or `false`.
7373+ # Matches if the value is either `true` or `false`. This is equivalent to `_Union(true, false)`.
7474+ # ```ruby
7575+ # _Boolean
7676+ # ```
6177 def _Boolean
6278 BooleanType::Instance
6379 end
64806565- # Nilable version of `_Boolean`
8181+ # Nilable version of `_Boolean`.
8282+ # ```ruby
8383+ # _Boolean?
8484+ # ```
6685 def _Boolean?
6786 NilableBooleanType
6887 end
69887089 # Matches if the value responds to `#call`.
9090+ # ```ruby
9191+ # _Callable
9292+ # ```
7193 def _Callable
7294 CallableType
7395 end
74967575- # Nilabl version of `_Callable`
9797+ # Nilable version of `_Callable`.
9898+ # ```ruby
9999+ # _Callable?
100100+ # ```
76101 def _Callable?
77102 NilableCallableType
78103 end
7910480105 # Matches if the value either the given class or a subclass of it.
8181- def _Class(...)
8282- ClassType.new(...)
106106+ # ```ruby
107107+ # _Class(ActiveRecord::Base)
108108+ # ```
109109+ def _Class(expected_class)
110110+ ClassType.new(expected_class)
83111 end
841128585- # Nilable version of `_Class`
113113+ # Nilable version of `_Class`.
114114+ # ```ruby
115115+ # _Class?(ActiveRecord::Base)
116116+ # ```
86117 def _Class?(...)
87118 NilableType.new(
88119 ClassType.new(...)
···90121 end
9112292123 # Similar to `_Intersection`, but allows you to specify attribute constraints as keyword arguments.
9393- # @example
9494- # _Constraint(Array, size: 1..3)
124124+ # ```ruby
125125+ # _Constraint(Array, size: 1..3)
126126+ # ```
95127 def _Constraint(...)
96128 ConstraintType.new(...)
97129 end
9813099131 # Nilable version of `_Constraint`
132132+ # ```ruby
133133+ # _Constraint?(Array, size: 1..3)
134134+ # ```
100135 def _Constraint?(...)
101136 NilableType.new(
102137 ConstraintType.new(...)
···104139 end
105140106141 # Matches if the value is a `Date` and matches the given constraints.
107107- # If you don't need any constraints, use `Date` instead of `_Date`.
142142+ # If you don't need any constraints, use `Date` instead of `_Date`. See also `_Constraint`.
143143+ # ```ruby
144144+ # _Date((Date.today)..)
145145+ # _Date(year: 2025)
146146+ # ```
108147 def _Date(...)
109148 _Constraint(Date, ...)
110149 end
111150112112- # Nilable version of `_Date`
151151+ # Nilable version of `_Date`.
113152 def _Date?(...)
114153 _Nilable(
115154 _Date(...)
116155 )
117156 end
118157119119- def _Deferred(...)
120120- DeferredType.new(...)
158158+ # Takes a type as a block so it can be resolved when needed. This is useful if declaring your type now would cause an error because constants haven’t been defined yet.
159159+ # ```ruby
160160+ # _Deferred { _Class(SomeFutureConstant) }
161161+ # ```
162162+ def _Deferred(&type)
163163+ DeferredType.new(&type)
164164+ end
165165+166166+ # Nilable version of `_Deferred`.
167167+ def _Deferred?(&type)
168168+ _Nilable(_Deferred(&type))
121169 end
122170123171 # Matches if the value is a descendant of the given class.
172172+ # ```ruby
173173+ # _Descendant(ActiveRecord::Base)
174174+ # ```
124175 def _Descendant(...)
125176 DescendantType.new(...)
126177 end
127178128128- # Nilable version of `_Descendant`
179179+ # Nilable version of `_Descendant`.
129180 def _Descendant?(...)
130181 NilableType.new(
131182 DescendantType.new(...)
···133184 end
134185135186 # Matches if the value is an `Enumerable` and all its elements match the given type.
136136- def _Enumerable(...)
137137- EnumerableType.new(...)
187187+ # ```ruby
188188+ # _Enumerable(String)
189189+ # ```
190190+ def _Enumerable(type)
191191+ EnumerableType.new(type)
138192 end
139193140140- # Nilable version of `_Enumerable`
194194+ # Nilable version of `_Enumerable`.
141195 def _Enumerable?(...)
142196 NilableType.new(
143197 EnumerableType.new(...)
144198 )
145199 end
146200147147- # Matches *"falsy"* values (`nil` and `false`).
201201+ # Matches *"falsy"* values (`nil` and `false`). This is equivalent to `_Nilable(false)` or `_Union(nil, false)`.
148202 def _Falsy
149203 FalsyType::Instance
150204 end
···152206 # Matches if the value is a `Float` and matches the given constraints.
153207 # You could use a `Range`, for example, as a constraint.
154208 # If you don't need a constraint, use `Float` instead of `_Float`.
209209+ # ```ruby
210210+ # _Float(5..10)
211211+ # ```
155212 def _Float(...)
156213 _Constraint(Float, ...)
157214 end
158215159159- # Nilable version of `_Float`
216216+ # Nilable version of `_Float`.
160217 def _Float?(...)
161218 _Nilable(
162219 _Float(...)