Sorbet

Commands

$ srb tc path # type checks

Editor Integration

Syntax

# typed: ignore -- not read by Sorbet at all
# typed: false -- just syntax and constant resolution
# typed: true -- type errors
# typed: strict -- all signatures required
# typed: strong -- everything you interact with must be typed

extend T::Sig # enables Sorbet in a class
MyConstant = T.let(value, type) # add types to constants
sig{…} # adds a signature above a method
params(positional_param: type, keyword_param: type) # specifies param types
.returns(type) # specifies return type
.void # no useful return value

T.nilable(type) # can be nil
T::Boolean # TrueClass or FalseClass
T::Array[element_type]
T::Hash[key_type, value_type]
T.any(types…)
MyTypeAlias = T.type_alias {…}

class MyStruct < T::Struct
  prop :my_prop, String

  # has a #serialize to go to a string-keyed hash; nils not included
end

module MyInterface
  extend T::Sig
  extend T::Helpers
  interface!

  sig { abstract.params(name: String).returns(String) }
  def greet(name)
  end
end

class MyImplementation
  extend T::Sig
  include MyInterface

  sig { override.params(name: String).returns(String) }
  def greet(name)
    "Hello, #{name}!"
  end
end