Sorbet Runtime Types
Installation
gem "sorbet-runtime"bundle installBasic Usage
require "sorbet-runtime"
class User::Create < ApplicationService
# Basic types - plain Ruby classes work directly!
arg :name, type: String
arg :age, type: Integer
# Nilable types (allows nil)
arg :email, type: T.nilable(String), optional: true
# Union types (multiple allowed types)
arg :status, type: T.any(String, Symbol), default: "pending"
# Typed arrays
arg :tags, type: T::Array[String], optional: true
# Boolean type
arg :active, type: T::Boolean, default: true
# Outputs with Sorbet types - plain classes work here too
output :user, type: User
output :metadata, type: Hash
step :create_user
step :build_metadata
private
def create_user
self.user = User.create!(
name: name,
age: age,
email: email,
status: status,
tags: tags || [],
active: active
)
end
def build_metadata
self.metadata = { created_at: Time.current }
end
endType Reference
Basic Types
Nilable Types
Union Types
Typed Arrays
Boolean Type
Complex Types
Important: Sorbet Types Validate Only
Validation Behavior
Combining with Tapioca
Error Messages
Full Example
Last updated