Overview
Type checking with Sorbet is composed of two key components:
srb
This is the command-line interface to Sorbet. It includes the core type checker, which analyzes a project statically (before the code runs) to report potential mistakes in the code. It also contains utilities to set up a project to work with Sorbet for the first time.
sorbet-runtime
This is the gem that enables adding type annotations to normal Ruby code. It exposes the top-level
T
namespace and thesig
method, which we’ll see more of in Signatures. It also dynamically type checks the code while it runs.
These two components are developed in tandem, and in fact compound each others’ guarantees. Sorbet makes predictions about the runtime, and the runtime enforces those predictions with contracts.
Here’s a taste of what Sorbet can do:
# typed: true
require 'sorbet-runtime'
class A
extend T::Sig
sig {params(x: Integer).returns(String)}
def bar(x)
x.to_s
end
end
def main
A.new.barr(91) # error: Typo!
A.new.bar("91") # error: Type mismatch!
end
What’s next?
-
Learn how to adopt Sorbet in an existing codebase.
Gradual Type Checking and Sorbet
Learn about how Sorbet works, and how it’s different from other type systems you might be familiar with.
-
Learn how to get more power out of Sorbet by enabling static checks.
-
Learn how to get more confidence out of Sorbet by enabling runtime checks.